vmx.c 353 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445
  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/moduleparam.h>
  29. #include <linux/mod_devicetable.h>
  30. #include <linux/trace_events.h>
  31. #include <linux/slab.h>
  32. #include <linux/tboot.h>
  33. #include <linux/hrtimer.h>
  34. #include <linux/frame.h>
  35. #include <linux/nospec.h>
  36. #include "kvm_cache_regs.h"
  37. #include "x86.h"
  38. #include <asm/cpu.h>
  39. #include <asm/io.h>
  40. #include <asm/desc.h>
  41. #include <asm/vmx.h>
  42. #include <asm/virtext.h>
  43. #include <asm/mce.h>
  44. #include <asm/fpu/internal.h>
  45. #include <asm/perf_event.h>
  46. #include <asm/debugreg.h>
  47. #include <asm/kexec.h>
  48. #include <asm/apic.h>
  49. #include <asm/irq_remapping.h>
  50. #include <asm/mmu_context.h>
  51. #include <asm/nospec-branch.h>
  52. #include "trace.h"
  53. #include "pmu.h"
  54. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  55. #define __ex_clear(x, reg) \
  56. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  57. MODULE_AUTHOR("Qumranet");
  58. MODULE_LICENSE("GPL");
  59. static const struct x86_cpu_id vmx_cpu_id[] = {
  60. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  61. {}
  62. };
  63. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  64. static bool __read_mostly enable_vpid = 1;
  65. module_param_named(vpid, enable_vpid, bool, 0444);
  66. static bool __read_mostly enable_vnmi = 1;
  67. module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
  68. static bool __read_mostly flexpriority_enabled = 1;
  69. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  70. static bool __read_mostly enable_ept = 1;
  71. module_param_named(ept, enable_ept, bool, S_IRUGO);
  72. static bool __read_mostly enable_unrestricted_guest = 1;
  73. module_param_named(unrestricted_guest,
  74. enable_unrestricted_guest, bool, S_IRUGO);
  75. static bool __read_mostly enable_ept_ad_bits = 1;
  76. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  77. static bool __read_mostly emulate_invalid_guest_state = true;
  78. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  79. static bool __read_mostly fasteoi = 1;
  80. module_param(fasteoi, bool, S_IRUGO);
  81. static bool __read_mostly enable_apicv = 1;
  82. module_param(enable_apicv, bool, S_IRUGO);
  83. static bool __read_mostly enable_shadow_vmcs = 1;
  84. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  85. /*
  86. * If nested=1, nested virtualization is supported, i.e., guests may use
  87. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  88. * use VMX instructions.
  89. */
  90. static bool __read_mostly nested = 0;
  91. module_param(nested, bool, S_IRUGO);
  92. static u64 __read_mostly host_xss;
  93. static bool __read_mostly enable_pml = 1;
  94. module_param_named(pml, enable_pml, bool, S_IRUGO);
  95. #define MSR_TYPE_R 1
  96. #define MSR_TYPE_W 2
  97. #define MSR_TYPE_RW 3
  98. #define MSR_BITMAP_MODE_X2APIC 1
  99. #define MSR_BITMAP_MODE_X2APIC_APICV 2
  100. #define MSR_BITMAP_MODE_LM 4
  101. #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
  102. /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
  103. static int __read_mostly cpu_preemption_timer_multi;
  104. static bool __read_mostly enable_preemption_timer = 1;
  105. #ifdef CONFIG_X86_64
  106. module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
  107. #endif
  108. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  109. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
  110. #define KVM_VM_CR0_ALWAYS_ON \
  111. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  112. #define KVM_CR4_GUEST_OWNED_BITS \
  113. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  114. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
  115. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  116. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  117. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  118. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  119. /*
  120. * Hyper-V requires all of these, so mark them as supported even though
  121. * they are just treated the same as all-context.
  122. */
  123. #define VMX_VPID_EXTENT_SUPPORTED_MASK \
  124. (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
  125. VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
  126. VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
  127. VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
  128. /*
  129. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  130. * ple_gap: upper bound on the amount of time between two successive
  131. * executions of PAUSE in a loop. Also indicate if ple enabled.
  132. * According to test, this time is usually smaller than 128 cycles.
  133. * ple_window: upper bound on the amount of time a guest is allowed to execute
  134. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  135. * less than 2^12 cycles
  136. * Time is measured based on a counter that runs at the same rate as the TSC,
  137. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  138. */
  139. #define KVM_VMX_DEFAULT_PLE_GAP 128
  140. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  141. #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
  142. #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
  143. #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
  144. INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
  145. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  146. module_param(ple_gap, int, S_IRUGO);
  147. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  148. module_param(ple_window, int, S_IRUGO);
  149. /* Default doubles per-vcpu window every exit. */
  150. static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
  151. module_param(ple_window_grow, int, S_IRUGO);
  152. /* Default resets per-vcpu window every exit to ple_window. */
  153. static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
  154. module_param(ple_window_shrink, int, S_IRUGO);
  155. /* Default is to compute the maximum so we can never overflow. */
  156. static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  157. static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  158. module_param(ple_window_max, int, S_IRUGO);
  159. extern const ulong vmx_return;
  160. #define NR_AUTOLOAD_MSRS 8
  161. struct vmcs {
  162. u32 revision_id;
  163. u32 abort;
  164. char data[0];
  165. };
  166. /*
  167. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  168. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  169. * loaded on this CPU (so we can clear them if the CPU goes down).
  170. */
  171. struct loaded_vmcs {
  172. struct vmcs *vmcs;
  173. struct vmcs *shadow_vmcs;
  174. int cpu;
  175. bool launched;
  176. bool nmi_known_unmasked;
  177. unsigned long vmcs_host_cr3; /* May not match real cr3 */
  178. unsigned long vmcs_host_cr4; /* May not match real cr4 */
  179. /* Support for vnmi-less CPUs */
  180. int soft_vnmi_blocked;
  181. ktime_t entry_time;
  182. s64 vnmi_blocked_time;
  183. unsigned long *msr_bitmap;
  184. struct list_head loaded_vmcss_on_cpu_link;
  185. };
  186. struct shared_msr_entry {
  187. unsigned index;
  188. u64 data;
  189. u64 mask;
  190. };
  191. /*
  192. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  193. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  194. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  195. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  196. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  197. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  198. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  199. * underlying hardware which will be used to run L2.
  200. * This structure is packed to ensure that its layout is identical across
  201. * machines (necessary for live migration).
  202. * If there are changes in this struct, VMCS12_REVISION must be changed.
  203. */
  204. typedef u64 natural_width;
  205. struct __packed vmcs12 {
  206. /* According to the Intel spec, a VMCS region must start with the
  207. * following two fields. Then follow implementation-specific data.
  208. */
  209. u32 revision_id;
  210. u32 abort;
  211. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  212. u32 padding[7]; /* room for future expansion */
  213. u64 io_bitmap_a;
  214. u64 io_bitmap_b;
  215. u64 msr_bitmap;
  216. u64 vm_exit_msr_store_addr;
  217. u64 vm_exit_msr_load_addr;
  218. u64 vm_entry_msr_load_addr;
  219. u64 tsc_offset;
  220. u64 virtual_apic_page_addr;
  221. u64 apic_access_addr;
  222. u64 posted_intr_desc_addr;
  223. u64 vm_function_control;
  224. u64 ept_pointer;
  225. u64 eoi_exit_bitmap0;
  226. u64 eoi_exit_bitmap1;
  227. u64 eoi_exit_bitmap2;
  228. u64 eoi_exit_bitmap3;
  229. u64 eptp_list_address;
  230. u64 xss_exit_bitmap;
  231. u64 guest_physical_address;
  232. u64 vmcs_link_pointer;
  233. u64 pml_address;
  234. u64 guest_ia32_debugctl;
  235. u64 guest_ia32_pat;
  236. u64 guest_ia32_efer;
  237. u64 guest_ia32_perf_global_ctrl;
  238. u64 guest_pdptr0;
  239. u64 guest_pdptr1;
  240. u64 guest_pdptr2;
  241. u64 guest_pdptr3;
  242. u64 guest_bndcfgs;
  243. u64 host_ia32_pat;
  244. u64 host_ia32_efer;
  245. u64 host_ia32_perf_global_ctrl;
  246. u64 padding64[8]; /* room for future expansion */
  247. /*
  248. * To allow migration of L1 (complete with its L2 guests) between
  249. * machines of different natural widths (32 or 64 bit), we cannot have
  250. * unsigned long fields with no explict size. We use u64 (aliased
  251. * natural_width) instead. Luckily, x86 is little-endian.
  252. */
  253. natural_width cr0_guest_host_mask;
  254. natural_width cr4_guest_host_mask;
  255. natural_width cr0_read_shadow;
  256. natural_width cr4_read_shadow;
  257. natural_width cr3_target_value0;
  258. natural_width cr3_target_value1;
  259. natural_width cr3_target_value2;
  260. natural_width cr3_target_value3;
  261. natural_width exit_qualification;
  262. natural_width guest_linear_address;
  263. natural_width guest_cr0;
  264. natural_width guest_cr3;
  265. natural_width guest_cr4;
  266. natural_width guest_es_base;
  267. natural_width guest_cs_base;
  268. natural_width guest_ss_base;
  269. natural_width guest_ds_base;
  270. natural_width guest_fs_base;
  271. natural_width guest_gs_base;
  272. natural_width guest_ldtr_base;
  273. natural_width guest_tr_base;
  274. natural_width guest_gdtr_base;
  275. natural_width guest_idtr_base;
  276. natural_width guest_dr7;
  277. natural_width guest_rsp;
  278. natural_width guest_rip;
  279. natural_width guest_rflags;
  280. natural_width guest_pending_dbg_exceptions;
  281. natural_width guest_sysenter_esp;
  282. natural_width guest_sysenter_eip;
  283. natural_width host_cr0;
  284. natural_width host_cr3;
  285. natural_width host_cr4;
  286. natural_width host_fs_base;
  287. natural_width host_gs_base;
  288. natural_width host_tr_base;
  289. natural_width host_gdtr_base;
  290. natural_width host_idtr_base;
  291. natural_width host_ia32_sysenter_esp;
  292. natural_width host_ia32_sysenter_eip;
  293. natural_width host_rsp;
  294. natural_width host_rip;
  295. natural_width paddingl[8]; /* room for future expansion */
  296. u32 pin_based_vm_exec_control;
  297. u32 cpu_based_vm_exec_control;
  298. u32 exception_bitmap;
  299. u32 page_fault_error_code_mask;
  300. u32 page_fault_error_code_match;
  301. u32 cr3_target_count;
  302. u32 vm_exit_controls;
  303. u32 vm_exit_msr_store_count;
  304. u32 vm_exit_msr_load_count;
  305. u32 vm_entry_controls;
  306. u32 vm_entry_msr_load_count;
  307. u32 vm_entry_intr_info_field;
  308. u32 vm_entry_exception_error_code;
  309. u32 vm_entry_instruction_len;
  310. u32 tpr_threshold;
  311. u32 secondary_vm_exec_control;
  312. u32 vm_instruction_error;
  313. u32 vm_exit_reason;
  314. u32 vm_exit_intr_info;
  315. u32 vm_exit_intr_error_code;
  316. u32 idt_vectoring_info_field;
  317. u32 idt_vectoring_error_code;
  318. u32 vm_exit_instruction_len;
  319. u32 vmx_instruction_info;
  320. u32 guest_es_limit;
  321. u32 guest_cs_limit;
  322. u32 guest_ss_limit;
  323. u32 guest_ds_limit;
  324. u32 guest_fs_limit;
  325. u32 guest_gs_limit;
  326. u32 guest_ldtr_limit;
  327. u32 guest_tr_limit;
  328. u32 guest_gdtr_limit;
  329. u32 guest_idtr_limit;
  330. u32 guest_es_ar_bytes;
  331. u32 guest_cs_ar_bytes;
  332. u32 guest_ss_ar_bytes;
  333. u32 guest_ds_ar_bytes;
  334. u32 guest_fs_ar_bytes;
  335. u32 guest_gs_ar_bytes;
  336. u32 guest_ldtr_ar_bytes;
  337. u32 guest_tr_ar_bytes;
  338. u32 guest_interruptibility_info;
  339. u32 guest_activity_state;
  340. u32 guest_sysenter_cs;
  341. u32 host_ia32_sysenter_cs;
  342. u32 vmx_preemption_timer_value;
  343. u32 padding32[7]; /* room for future expansion */
  344. u16 virtual_processor_id;
  345. u16 posted_intr_nv;
  346. u16 guest_es_selector;
  347. u16 guest_cs_selector;
  348. u16 guest_ss_selector;
  349. u16 guest_ds_selector;
  350. u16 guest_fs_selector;
  351. u16 guest_gs_selector;
  352. u16 guest_ldtr_selector;
  353. u16 guest_tr_selector;
  354. u16 guest_intr_status;
  355. u16 guest_pml_index;
  356. u16 host_es_selector;
  357. u16 host_cs_selector;
  358. u16 host_ss_selector;
  359. u16 host_ds_selector;
  360. u16 host_fs_selector;
  361. u16 host_gs_selector;
  362. u16 host_tr_selector;
  363. };
  364. /*
  365. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  366. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  367. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  368. */
  369. #define VMCS12_REVISION 0x11e57ed0
  370. /*
  371. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  372. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  373. * current implementation, 4K are reserved to avoid future complications.
  374. */
  375. #define VMCS12_SIZE 0x1000
  376. /*
  377. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  378. * supported VMCS12 field encoding.
  379. */
  380. #define VMCS12_MAX_FIELD_INDEX 0x17
  381. /*
  382. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  383. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  384. */
  385. struct nested_vmx {
  386. /* Has the level1 guest done vmxon? */
  387. bool vmxon;
  388. gpa_t vmxon_ptr;
  389. bool pml_full;
  390. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  391. gpa_t current_vmptr;
  392. /*
  393. * Cache of the guest's VMCS, existing outside of guest memory.
  394. * Loaded from guest memory during VMPTRLD. Flushed to guest
  395. * memory during VMCLEAR and VMPTRLD.
  396. */
  397. struct vmcs12 *cached_vmcs12;
  398. /*
  399. * Indicates if the shadow vmcs must be updated with the
  400. * data hold by vmcs12
  401. */
  402. bool sync_shadow_vmcs;
  403. bool dirty_vmcs12;
  404. bool change_vmcs01_virtual_x2apic_mode;
  405. /* L2 must run next, and mustn't decide to exit to L1. */
  406. bool nested_run_pending;
  407. struct loaded_vmcs vmcs02;
  408. /*
  409. * Guest pages referred to in the vmcs02 with host-physical
  410. * pointers, so we must keep them pinned while L2 runs.
  411. */
  412. struct page *apic_access_page;
  413. struct page *virtual_apic_page;
  414. struct page *pi_desc_page;
  415. struct pi_desc *pi_desc;
  416. bool pi_pending;
  417. u16 posted_intr_nv;
  418. struct hrtimer preemption_timer;
  419. bool preemption_timer_expired;
  420. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  421. u64 vmcs01_debugctl;
  422. u16 vpid02;
  423. u16 last_vpid;
  424. /*
  425. * We only store the "true" versions of the VMX capability MSRs. We
  426. * generate the "non-true" versions by setting the must-be-1 bits
  427. * according to the SDM.
  428. */
  429. u32 nested_vmx_procbased_ctls_low;
  430. u32 nested_vmx_procbased_ctls_high;
  431. u32 nested_vmx_secondary_ctls_low;
  432. u32 nested_vmx_secondary_ctls_high;
  433. u32 nested_vmx_pinbased_ctls_low;
  434. u32 nested_vmx_pinbased_ctls_high;
  435. u32 nested_vmx_exit_ctls_low;
  436. u32 nested_vmx_exit_ctls_high;
  437. u32 nested_vmx_entry_ctls_low;
  438. u32 nested_vmx_entry_ctls_high;
  439. u32 nested_vmx_misc_low;
  440. u32 nested_vmx_misc_high;
  441. u32 nested_vmx_ept_caps;
  442. u32 nested_vmx_vpid_caps;
  443. u64 nested_vmx_basic;
  444. u64 nested_vmx_cr0_fixed0;
  445. u64 nested_vmx_cr0_fixed1;
  446. u64 nested_vmx_cr4_fixed0;
  447. u64 nested_vmx_cr4_fixed1;
  448. u64 nested_vmx_vmcs_enum;
  449. u64 nested_vmx_vmfunc_controls;
  450. /* SMM related state */
  451. struct {
  452. /* in VMX operation on SMM entry? */
  453. bool vmxon;
  454. /* in guest mode on SMM entry? */
  455. bool guest_mode;
  456. } smm;
  457. };
  458. #define POSTED_INTR_ON 0
  459. #define POSTED_INTR_SN 1
  460. /* Posted-Interrupt Descriptor */
  461. struct pi_desc {
  462. u32 pir[8]; /* Posted interrupt requested */
  463. union {
  464. struct {
  465. /* bit 256 - Outstanding Notification */
  466. u16 on : 1,
  467. /* bit 257 - Suppress Notification */
  468. sn : 1,
  469. /* bit 271:258 - Reserved */
  470. rsvd_1 : 14;
  471. /* bit 279:272 - Notification Vector */
  472. u8 nv;
  473. /* bit 287:280 - Reserved */
  474. u8 rsvd_2;
  475. /* bit 319:288 - Notification Destination */
  476. u32 ndst;
  477. };
  478. u64 control;
  479. };
  480. u32 rsvd[6];
  481. } __aligned(64);
  482. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  483. {
  484. return test_and_set_bit(POSTED_INTR_ON,
  485. (unsigned long *)&pi_desc->control);
  486. }
  487. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  488. {
  489. return test_and_clear_bit(POSTED_INTR_ON,
  490. (unsigned long *)&pi_desc->control);
  491. }
  492. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  493. {
  494. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  495. }
  496. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  497. {
  498. return clear_bit(POSTED_INTR_SN,
  499. (unsigned long *)&pi_desc->control);
  500. }
  501. static inline void pi_set_sn(struct pi_desc *pi_desc)
  502. {
  503. return set_bit(POSTED_INTR_SN,
  504. (unsigned long *)&pi_desc->control);
  505. }
  506. static inline void pi_clear_on(struct pi_desc *pi_desc)
  507. {
  508. clear_bit(POSTED_INTR_ON,
  509. (unsigned long *)&pi_desc->control);
  510. }
  511. static inline int pi_test_on(struct pi_desc *pi_desc)
  512. {
  513. return test_bit(POSTED_INTR_ON,
  514. (unsigned long *)&pi_desc->control);
  515. }
  516. static inline int pi_test_sn(struct pi_desc *pi_desc)
  517. {
  518. return test_bit(POSTED_INTR_SN,
  519. (unsigned long *)&pi_desc->control);
  520. }
  521. struct vcpu_vmx {
  522. struct kvm_vcpu vcpu;
  523. unsigned long host_rsp;
  524. u8 fail;
  525. u8 msr_bitmap_mode;
  526. u32 exit_intr_info;
  527. u32 idt_vectoring_info;
  528. ulong rflags;
  529. struct shared_msr_entry *guest_msrs;
  530. int nmsrs;
  531. int save_nmsrs;
  532. unsigned long host_idt_base;
  533. #ifdef CONFIG_X86_64
  534. u64 msr_host_kernel_gs_base;
  535. u64 msr_guest_kernel_gs_base;
  536. #endif
  537. u64 arch_capabilities;
  538. u64 spec_ctrl;
  539. u32 vm_entry_controls_shadow;
  540. u32 vm_exit_controls_shadow;
  541. u32 secondary_exec_control;
  542. /*
  543. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  544. * non-nested (L1) guest, it always points to vmcs01. For a nested
  545. * guest (L2), it points to a different VMCS.
  546. */
  547. struct loaded_vmcs vmcs01;
  548. struct loaded_vmcs *loaded_vmcs;
  549. bool __launched; /* temporary, used in vmx_vcpu_run */
  550. struct msr_autoload {
  551. unsigned nr;
  552. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  553. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  554. } msr_autoload;
  555. struct {
  556. int loaded;
  557. u16 fs_sel, gs_sel, ldt_sel;
  558. #ifdef CONFIG_X86_64
  559. u16 ds_sel, es_sel;
  560. #endif
  561. int gs_ldt_reload_needed;
  562. int fs_reload_needed;
  563. u64 msr_host_bndcfgs;
  564. } host_state;
  565. struct {
  566. int vm86_active;
  567. ulong save_rflags;
  568. struct kvm_segment segs[8];
  569. } rmode;
  570. struct {
  571. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  572. struct kvm_save_segment {
  573. u16 selector;
  574. unsigned long base;
  575. u32 limit;
  576. u32 ar;
  577. } seg[8];
  578. } segment_cache;
  579. int vpid;
  580. bool emulation_required;
  581. u32 exit_reason;
  582. /* Posted interrupt descriptor */
  583. struct pi_desc pi_desc;
  584. /* Support for a guest hypervisor (nested VMX) */
  585. struct nested_vmx nested;
  586. /* Dynamic PLE window. */
  587. int ple_window;
  588. bool ple_window_dirty;
  589. /* Support for PML */
  590. #define PML_ENTITY_NUM 512
  591. struct page *pml_pg;
  592. /* apic deadline value in host tsc */
  593. u64 hv_deadline_tsc;
  594. u64 current_tsc_ratio;
  595. u32 host_pkru;
  596. unsigned long host_debugctlmsr;
  597. /*
  598. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  599. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  600. * in msr_ia32_feature_control_valid_bits.
  601. */
  602. u64 msr_ia32_feature_control;
  603. u64 msr_ia32_feature_control_valid_bits;
  604. };
  605. enum segment_cache_field {
  606. SEG_FIELD_SEL = 0,
  607. SEG_FIELD_BASE = 1,
  608. SEG_FIELD_LIMIT = 2,
  609. SEG_FIELD_AR = 3,
  610. SEG_FIELD_NR = 4
  611. };
  612. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  613. {
  614. return container_of(vcpu, struct vcpu_vmx, vcpu);
  615. }
  616. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  617. {
  618. return &(to_vmx(vcpu)->pi_desc);
  619. }
  620. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  621. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  622. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  623. #define FIELD64(number, name) \
  624. FIELD(number, name), \
  625. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  626. static u16 shadow_read_only_fields[] = {
  627. #define SHADOW_FIELD_RO(x) x,
  628. #include "vmx_shadow_fields.h"
  629. };
  630. static int max_shadow_read_only_fields =
  631. ARRAY_SIZE(shadow_read_only_fields);
  632. static u16 shadow_read_write_fields[] = {
  633. #define SHADOW_FIELD_RW(x) x,
  634. #include "vmx_shadow_fields.h"
  635. };
  636. static int max_shadow_read_write_fields =
  637. ARRAY_SIZE(shadow_read_write_fields);
  638. static const unsigned short vmcs_field_to_offset_table[] = {
  639. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  640. FIELD(POSTED_INTR_NV, posted_intr_nv),
  641. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  642. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  643. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  644. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  645. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  646. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  647. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  648. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  649. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  650. FIELD(GUEST_PML_INDEX, guest_pml_index),
  651. FIELD(HOST_ES_SELECTOR, host_es_selector),
  652. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  653. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  654. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  655. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  656. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  657. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  658. FIELD64(IO_BITMAP_A, io_bitmap_a),
  659. FIELD64(IO_BITMAP_B, io_bitmap_b),
  660. FIELD64(MSR_BITMAP, msr_bitmap),
  661. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  662. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  663. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  664. FIELD64(TSC_OFFSET, tsc_offset),
  665. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  666. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  667. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  668. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  669. FIELD64(EPT_POINTER, ept_pointer),
  670. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  671. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  672. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  673. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  674. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  675. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  676. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  677. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  678. FIELD64(PML_ADDRESS, pml_address),
  679. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  680. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  681. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  682. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  683. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  684. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  685. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  686. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  687. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  688. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  689. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  690. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  691. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  692. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  693. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  694. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  695. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  696. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  697. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  698. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  699. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  700. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  701. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  702. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  703. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  704. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  705. FIELD(TPR_THRESHOLD, tpr_threshold),
  706. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  707. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  708. FIELD(VM_EXIT_REASON, vm_exit_reason),
  709. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  710. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  711. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  712. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  713. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  714. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  715. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  716. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  717. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  718. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  719. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  720. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  721. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  722. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  723. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  724. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  725. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  726. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  727. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  728. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  729. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  730. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  731. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  732. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  733. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  734. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  735. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  736. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  737. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  738. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  739. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  740. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  741. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  742. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  743. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  744. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  745. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  746. FIELD(EXIT_QUALIFICATION, exit_qualification),
  747. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  748. FIELD(GUEST_CR0, guest_cr0),
  749. FIELD(GUEST_CR3, guest_cr3),
  750. FIELD(GUEST_CR4, guest_cr4),
  751. FIELD(GUEST_ES_BASE, guest_es_base),
  752. FIELD(GUEST_CS_BASE, guest_cs_base),
  753. FIELD(GUEST_SS_BASE, guest_ss_base),
  754. FIELD(GUEST_DS_BASE, guest_ds_base),
  755. FIELD(GUEST_FS_BASE, guest_fs_base),
  756. FIELD(GUEST_GS_BASE, guest_gs_base),
  757. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  758. FIELD(GUEST_TR_BASE, guest_tr_base),
  759. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  760. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  761. FIELD(GUEST_DR7, guest_dr7),
  762. FIELD(GUEST_RSP, guest_rsp),
  763. FIELD(GUEST_RIP, guest_rip),
  764. FIELD(GUEST_RFLAGS, guest_rflags),
  765. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  766. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  767. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  768. FIELD(HOST_CR0, host_cr0),
  769. FIELD(HOST_CR3, host_cr3),
  770. FIELD(HOST_CR4, host_cr4),
  771. FIELD(HOST_FS_BASE, host_fs_base),
  772. FIELD(HOST_GS_BASE, host_gs_base),
  773. FIELD(HOST_TR_BASE, host_tr_base),
  774. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  775. FIELD(HOST_IDTR_BASE, host_idtr_base),
  776. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  777. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  778. FIELD(HOST_RSP, host_rsp),
  779. FIELD(HOST_RIP, host_rip),
  780. };
  781. static inline short vmcs_field_to_offset(unsigned long field)
  782. {
  783. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  784. unsigned short offset;
  785. unsigned index;
  786. if (field >> 15)
  787. return -ENOENT;
  788. index = ROL16(field, 6);
  789. if (index >= size)
  790. return -ENOENT;
  791. index = array_index_nospec(index, size);
  792. offset = vmcs_field_to_offset_table[index];
  793. if (offset == 0)
  794. return -ENOENT;
  795. return offset;
  796. }
  797. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  798. {
  799. return to_vmx(vcpu)->nested.cached_vmcs12;
  800. }
  801. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  802. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  803. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  804. static bool vmx_xsaves_supported(void);
  805. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  806. struct kvm_segment *var, int seg);
  807. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  808. struct kvm_segment *var, int seg);
  809. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  810. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  811. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  812. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  813. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  814. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  815. u16 error_code);
  816. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  817. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  818. u32 msr, int type);
  819. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  820. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  821. /*
  822. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  823. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  824. */
  825. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  826. /*
  827. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  828. * can find which vCPU should be waken up.
  829. */
  830. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  831. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  832. enum {
  833. VMX_VMREAD_BITMAP,
  834. VMX_VMWRITE_BITMAP,
  835. VMX_BITMAP_NR
  836. };
  837. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  838. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  839. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  840. static bool cpu_has_load_ia32_efer;
  841. static bool cpu_has_load_perf_global_ctrl;
  842. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  843. static DEFINE_SPINLOCK(vmx_vpid_lock);
  844. static struct vmcs_config {
  845. int size;
  846. int order;
  847. u32 basic_cap;
  848. u32 revision_id;
  849. u32 pin_based_exec_ctrl;
  850. u32 cpu_based_exec_ctrl;
  851. u32 cpu_based_2nd_exec_ctrl;
  852. u32 vmexit_ctrl;
  853. u32 vmentry_ctrl;
  854. } vmcs_config;
  855. static struct vmx_capability {
  856. u32 ept;
  857. u32 vpid;
  858. } vmx_capability;
  859. #define VMX_SEGMENT_FIELD(seg) \
  860. [VCPU_SREG_##seg] = { \
  861. .selector = GUEST_##seg##_SELECTOR, \
  862. .base = GUEST_##seg##_BASE, \
  863. .limit = GUEST_##seg##_LIMIT, \
  864. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  865. }
  866. static const struct kvm_vmx_segment_field {
  867. unsigned selector;
  868. unsigned base;
  869. unsigned limit;
  870. unsigned ar_bytes;
  871. } kvm_vmx_segment_fields[] = {
  872. VMX_SEGMENT_FIELD(CS),
  873. VMX_SEGMENT_FIELD(DS),
  874. VMX_SEGMENT_FIELD(ES),
  875. VMX_SEGMENT_FIELD(FS),
  876. VMX_SEGMENT_FIELD(GS),
  877. VMX_SEGMENT_FIELD(SS),
  878. VMX_SEGMENT_FIELD(TR),
  879. VMX_SEGMENT_FIELD(LDTR),
  880. };
  881. static u64 host_efer;
  882. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  883. /*
  884. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  885. * away by decrementing the array size.
  886. */
  887. static const u32 vmx_msr_index[] = {
  888. #ifdef CONFIG_X86_64
  889. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  890. #endif
  891. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  892. };
  893. static inline bool is_exception_n(u32 intr_info, u8 vector)
  894. {
  895. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  896. INTR_INFO_VALID_MASK)) ==
  897. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  898. }
  899. static inline bool is_debug(u32 intr_info)
  900. {
  901. return is_exception_n(intr_info, DB_VECTOR);
  902. }
  903. static inline bool is_breakpoint(u32 intr_info)
  904. {
  905. return is_exception_n(intr_info, BP_VECTOR);
  906. }
  907. static inline bool is_page_fault(u32 intr_info)
  908. {
  909. return is_exception_n(intr_info, PF_VECTOR);
  910. }
  911. static inline bool is_no_device(u32 intr_info)
  912. {
  913. return is_exception_n(intr_info, NM_VECTOR);
  914. }
  915. static inline bool is_invalid_opcode(u32 intr_info)
  916. {
  917. return is_exception_n(intr_info, UD_VECTOR);
  918. }
  919. static inline bool is_external_interrupt(u32 intr_info)
  920. {
  921. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  922. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  923. }
  924. static inline bool is_machine_check(u32 intr_info)
  925. {
  926. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  927. INTR_INFO_VALID_MASK)) ==
  928. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  929. }
  930. static inline bool cpu_has_vmx_msr_bitmap(void)
  931. {
  932. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  933. }
  934. static inline bool cpu_has_vmx_tpr_shadow(void)
  935. {
  936. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  937. }
  938. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  939. {
  940. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  941. }
  942. static inline bool cpu_has_secondary_exec_ctrls(void)
  943. {
  944. return vmcs_config.cpu_based_exec_ctrl &
  945. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  946. }
  947. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  948. {
  949. return vmcs_config.cpu_based_2nd_exec_ctrl &
  950. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  951. }
  952. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  953. {
  954. return vmcs_config.cpu_based_2nd_exec_ctrl &
  955. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  956. }
  957. static inline bool cpu_has_vmx_apic_register_virt(void)
  958. {
  959. return vmcs_config.cpu_based_2nd_exec_ctrl &
  960. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  961. }
  962. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  963. {
  964. return vmcs_config.cpu_based_2nd_exec_ctrl &
  965. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  966. }
  967. /*
  968. * Comment's format: document - errata name - stepping - processor name.
  969. * Refer from
  970. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  971. */
  972. static u32 vmx_preemption_cpu_tfms[] = {
  973. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  974. 0x000206E6,
  975. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  976. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  977. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  978. 0x00020652,
  979. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  980. 0x00020655,
  981. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  982. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  983. /*
  984. * 320767.pdf - AAP86 - B1 -
  985. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  986. */
  987. 0x000106E5,
  988. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  989. 0x000106A0,
  990. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  991. 0x000106A1,
  992. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  993. 0x000106A4,
  994. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  995. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  996. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  997. 0x000106A5,
  998. };
  999. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1000. {
  1001. u32 eax = cpuid_eax(0x00000001), i;
  1002. /* Clear the reserved bits */
  1003. eax &= ~(0x3U << 14 | 0xfU << 28);
  1004. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1005. if (eax == vmx_preemption_cpu_tfms[i])
  1006. return true;
  1007. return false;
  1008. }
  1009. static inline bool cpu_has_vmx_preemption_timer(void)
  1010. {
  1011. return vmcs_config.pin_based_exec_ctrl &
  1012. PIN_BASED_VMX_PREEMPTION_TIMER;
  1013. }
  1014. static inline bool cpu_has_vmx_posted_intr(void)
  1015. {
  1016. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1017. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1018. }
  1019. static inline bool cpu_has_vmx_apicv(void)
  1020. {
  1021. return cpu_has_vmx_apic_register_virt() &&
  1022. cpu_has_vmx_virtual_intr_delivery() &&
  1023. cpu_has_vmx_posted_intr();
  1024. }
  1025. static inline bool cpu_has_vmx_flexpriority(void)
  1026. {
  1027. return cpu_has_vmx_tpr_shadow() &&
  1028. cpu_has_vmx_virtualize_apic_accesses();
  1029. }
  1030. static inline bool cpu_has_vmx_ept_execute_only(void)
  1031. {
  1032. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1033. }
  1034. static inline bool cpu_has_vmx_ept_2m_page(void)
  1035. {
  1036. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1037. }
  1038. static inline bool cpu_has_vmx_ept_1g_page(void)
  1039. {
  1040. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1041. }
  1042. static inline bool cpu_has_vmx_ept_4levels(void)
  1043. {
  1044. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1045. }
  1046. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1047. {
  1048. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1049. }
  1050. static inline bool cpu_has_vmx_ept_5levels(void)
  1051. {
  1052. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1053. }
  1054. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1055. {
  1056. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1057. }
  1058. static inline bool cpu_has_vmx_invept_context(void)
  1059. {
  1060. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1061. }
  1062. static inline bool cpu_has_vmx_invept_global(void)
  1063. {
  1064. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1065. }
  1066. static inline bool cpu_has_vmx_invvpid_single(void)
  1067. {
  1068. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1069. }
  1070. static inline bool cpu_has_vmx_invvpid_global(void)
  1071. {
  1072. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1073. }
  1074. static inline bool cpu_has_vmx_invvpid(void)
  1075. {
  1076. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1077. }
  1078. static inline bool cpu_has_vmx_ept(void)
  1079. {
  1080. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1081. SECONDARY_EXEC_ENABLE_EPT;
  1082. }
  1083. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1084. {
  1085. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1086. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1087. }
  1088. static inline bool cpu_has_vmx_ple(void)
  1089. {
  1090. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1091. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1092. }
  1093. static inline bool cpu_has_vmx_basic_inout(void)
  1094. {
  1095. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1096. }
  1097. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1098. {
  1099. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1100. }
  1101. static inline bool cpu_has_vmx_vpid(void)
  1102. {
  1103. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1104. SECONDARY_EXEC_ENABLE_VPID;
  1105. }
  1106. static inline bool cpu_has_vmx_rdtscp(void)
  1107. {
  1108. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1109. SECONDARY_EXEC_RDTSCP;
  1110. }
  1111. static inline bool cpu_has_vmx_invpcid(void)
  1112. {
  1113. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1114. SECONDARY_EXEC_ENABLE_INVPCID;
  1115. }
  1116. static inline bool cpu_has_virtual_nmis(void)
  1117. {
  1118. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1119. }
  1120. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1121. {
  1122. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1123. SECONDARY_EXEC_WBINVD_EXITING;
  1124. }
  1125. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1126. {
  1127. u64 vmx_msr;
  1128. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1129. /* check if the cpu supports writing r/o exit information fields */
  1130. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1131. return false;
  1132. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1133. SECONDARY_EXEC_SHADOW_VMCS;
  1134. }
  1135. static inline bool cpu_has_vmx_pml(void)
  1136. {
  1137. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1138. }
  1139. static inline bool cpu_has_vmx_tsc_scaling(void)
  1140. {
  1141. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1142. SECONDARY_EXEC_TSC_SCALING;
  1143. }
  1144. static inline bool cpu_has_vmx_vmfunc(void)
  1145. {
  1146. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1147. SECONDARY_EXEC_ENABLE_VMFUNC;
  1148. }
  1149. static inline bool report_flexpriority(void)
  1150. {
  1151. return flexpriority_enabled;
  1152. }
  1153. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1154. {
  1155. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
  1156. }
  1157. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1158. {
  1159. return vmcs12->cpu_based_vm_exec_control & bit;
  1160. }
  1161. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1162. {
  1163. return (vmcs12->cpu_based_vm_exec_control &
  1164. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1165. (vmcs12->secondary_vm_exec_control & bit);
  1166. }
  1167. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1168. {
  1169. return vmcs12->pin_based_vm_exec_control &
  1170. PIN_BASED_VMX_PREEMPTION_TIMER;
  1171. }
  1172. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1173. {
  1174. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1175. }
  1176. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1177. {
  1178. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1179. }
  1180. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1181. {
  1182. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1183. }
  1184. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1185. {
  1186. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1187. }
  1188. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1189. {
  1190. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1191. }
  1192. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1193. {
  1194. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1195. }
  1196. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1197. {
  1198. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1199. }
  1200. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1201. {
  1202. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1203. }
  1204. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1205. {
  1206. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1207. }
  1208. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1209. {
  1210. return nested_cpu_has_vmfunc(vmcs12) &&
  1211. (vmcs12->vm_function_control &
  1212. VMX_VMFUNC_EPTP_SWITCHING);
  1213. }
  1214. static inline bool is_nmi(u32 intr_info)
  1215. {
  1216. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1217. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1218. }
  1219. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1220. u32 exit_intr_info,
  1221. unsigned long exit_qualification);
  1222. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1223. struct vmcs12 *vmcs12,
  1224. u32 reason, unsigned long qualification);
  1225. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1226. {
  1227. int i;
  1228. for (i = 0; i < vmx->nmsrs; ++i)
  1229. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1230. return i;
  1231. return -1;
  1232. }
  1233. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  1234. {
  1235. struct {
  1236. u64 vpid : 16;
  1237. u64 rsvd : 48;
  1238. u64 gva;
  1239. } operand = { vpid, 0, gva };
  1240. asm volatile (__ex(ASM_VMX_INVVPID)
  1241. /* CF==1 or ZF==1 --> rc = -1 */
  1242. "; ja 1f ; ud2 ; 1:"
  1243. : : "a"(&operand), "c"(ext) : "cc", "memory");
  1244. }
  1245. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1246. {
  1247. struct {
  1248. u64 eptp, gpa;
  1249. } operand = {eptp, gpa};
  1250. asm volatile (__ex(ASM_VMX_INVEPT)
  1251. /* CF==1 or ZF==1 --> rc = -1 */
  1252. "; ja 1f ; ud2 ; 1:\n"
  1253. : : "a" (&operand), "c" (ext) : "cc", "memory");
  1254. }
  1255. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1256. {
  1257. int i;
  1258. i = __find_msr_index(vmx, msr);
  1259. if (i >= 0)
  1260. return &vmx->guest_msrs[i];
  1261. return NULL;
  1262. }
  1263. static void vmcs_clear(struct vmcs *vmcs)
  1264. {
  1265. u64 phys_addr = __pa(vmcs);
  1266. u8 error;
  1267. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  1268. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1269. : "cc", "memory");
  1270. if (error)
  1271. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1272. vmcs, phys_addr);
  1273. }
  1274. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1275. {
  1276. vmcs_clear(loaded_vmcs->vmcs);
  1277. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1278. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1279. loaded_vmcs->cpu = -1;
  1280. loaded_vmcs->launched = 0;
  1281. }
  1282. static void vmcs_load(struct vmcs *vmcs)
  1283. {
  1284. u64 phys_addr = __pa(vmcs);
  1285. u8 error;
  1286. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  1287. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1288. : "cc", "memory");
  1289. if (error)
  1290. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1291. vmcs, phys_addr);
  1292. }
  1293. #ifdef CONFIG_KEXEC_CORE
  1294. /*
  1295. * This bitmap is used to indicate whether the vmclear
  1296. * operation is enabled on all cpus. All disabled by
  1297. * default.
  1298. */
  1299. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1300. static inline void crash_enable_local_vmclear(int cpu)
  1301. {
  1302. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1303. }
  1304. static inline void crash_disable_local_vmclear(int cpu)
  1305. {
  1306. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1307. }
  1308. static inline int crash_local_vmclear_enabled(int cpu)
  1309. {
  1310. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1311. }
  1312. static void crash_vmclear_local_loaded_vmcss(void)
  1313. {
  1314. int cpu = raw_smp_processor_id();
  1315. struct loaded_vmcs *v;
  1316. if (!crash_local_vmclear_enabled(cpu))
  1317. return;
  1318. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1319. loaded_vmcss_on_cpu_link)
  1320. vmcs_clear(v->vmcs);
  1321. }
  1322. #else
  1323. static inline void crash_enable_local_vmclear(int cpu) { }
  1324. static inline void crash_disable_local_vmclear(int cpu) { }
  1325. #endif /* CONFIG_KEXEC_CORE */
  1326. static void __loaded_vmcs_clear(void *arg)
  1327. {
  1328. struct loaded_vmcs *loaded_vmcs = arg;
  1329. int cpu = raw_smp_processor_id();
  1330. if (loaded_vmcs->cpu != cpu)
  1331. return; /* vcpu migration can race with cpu offline */
  1332. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1333. per_cpu(current_vmcs, cpu) = NULL;
  1334. crash_disable_local_vmclear(cpu);
  1335. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1336. /*
  1337. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1338. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1339. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1340. * then adds the vmcs into percpu list before it is deleted.
  1341. */
  1342. smp_wmb();
  1343. loaded_vmcs_init(loaded_vmcs);
  1344. crash_enable_local_vmclear(cpu);
  1345. }
  1346. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1347. {
  1348. int cpu = loaded_vmcs->cpu;
  1349. if (cpu != -1)
  1350. smp_call_function_single(cpu,
  1351. __loaded_vmcs_clear, loaded_vmcs, 1);
  1352. }
  1353. static inline void vpid_sync_vcpu_single(int vpid)
  1354. {
  1355. if (vpid == 0)
  1356. return;
  1357. if (cpu_has_vmx_invvpid_single())
  1358. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1359. }
  1360. static inline void vpid_sync_vcpu_global(void)
  1361. {
  1362. if (cpu_has_vmx_invvpid_global())
  1363. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1364. }
  1365. static inline void vpid_sync_context(int vpid)
  1366. {
  1367. if (cpu_has_vmx_invvpid_single())
  1368. vpid_sync_vcpu_single(vpid);
  1369. else
  1370. vpid_sync_vcpu_global();
  1371. }
  1372. static inline void ept_sync_global(void)
  1373. {
  1374. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1375. }
  1376. static inline void ept_sync_context(u64 eptp)
  1377. {
  1378. if (cpu_has_vmx_invept_context())
  1379. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1380. else
  1381. ept_sync_global();
  1382. }
  1383. static __always_inline void vmcs_check16(unsigned long field)
  1384. {
  1385. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1386. "16-bit accessor invalid for 64-bit field");
  1387. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1388. "16-bit accessor invalid for 64-bit high field");
  1389. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1390. "16-bit accessor invalid for 32-bit high field");
  1391. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1392. "16-bit accessor invalid for natural width field");
  1393. }
  1394. static __always_inline void vmcs_check32(unsigned long field)
  1395. {
  1396. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1397. "32-bit accessor invalid for 16-bit field");
  1398. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1399. "32-bit accessor invalid for natural width field");
  1400. }
  1401. static __always_inline void vmcs_check64(unsigned long field)
  1402. {
  1403. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1404. "64-bit accessor invalid for 16-bit field");
  1405. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1406. "64-bit accessor invalid for 64-bit high field");
  1407. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1408. "64-bit accessor invalid for 32-bit field");
  1409. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1410. "64-bit accessor invalid for natural width field");
  1411. }
  1412. static __always_inline void vmcs_checkl(unsigned long field)
  1413. {
  1414. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1415. "Natural width accessor invalid for 16-bit field");
  1416. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1417. "Natural width accessor invalid for 64-bit field");
  1418. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1419. "Natural width accessor invalid for 64-bit high field");
  1420. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1421. "Natural width accessor invalid for 32-bit field");
  1422. }
  1423. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  1424. {
  1425. unsigned long value;
  1426. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1427. : "=a"(value) : "d"(field) : "cc");
  1428. return value;
  1429. }
  1430. static __always_inline u16 vmcs_read16(unsigned long field)
  1431. {
  1432. vmcs_check16(field);
  1433. return __vmcs_readl(field);
  1434. }
  1435. static __always_inline u32 vmcs_read32(unsigned long field)
  1436. {
  1437. vmcs_check32(field);
  1438. return __vmcs_readl(field);
  1439. }
  1440. static __always_inline u64 vmcs_read64(unsigned long field)
  1441. {
  1442. vmcs_check64(field);
  1443. #ifdef CONFIG_X86_64
  1444. return __vmcs_readl(field);
  1445. #else
  1446. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  1447. #endif
  1448. }
  1449. static __always_inline unsigned long vmcs_readl(unsigned long field)
  1450. {
  1451. vmcs_checkl(field);
  1452. return __vmcs_readl(field);
  1453. }
  1454. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  1455. {
  1456. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  1457. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  1458. dump_stack();
  1459. }
  1460. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  1461. {
  1462. u8 error;
  1463. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  1464. : "=q"(error) : "a"(value), "d"(field) : "cc");
  1465. if (unlikely(error))
  1466. vmwrite_error(field, value);
  1467. }
  1468. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  1469. {
  1470. vmcs_check16(field);
  1471. __vmcs_writel(field, value);
  1472. }
  1473. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  1474. {
  1475. vmcs_check32(field);
  1476. __vmcs_writel(field, value);
  1477. }
  1478. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  1479. {
  1480. vmcs_check64(field);
  1481. __vmcs_writel(field, value);
  1482. #ifndef CONFIG_X86_64
  1483. asm volatile ("");
  1484. __vmcs_writel(field+1, value >> 32);
  1485. #endif
  1486. }
  1487. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  1488. {
  1489. vmcs_checkl(field);
  1490. __vmcs_writel(field, value);
  1491. }
  1492. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  1493. {
  1494. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1495. "vmcs_clear_bits does not support 64-bit fields");
  1496. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  1497. }
  1498. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  1499. {
  1500. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  1501. "vmcs_set_bits does not support 64-bit fields");
  1502. __vmcs_writel(field, __vmcs_readl(field) | mask);
  1503. }
  1504. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  1505. {
  1506. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  1507. }
  1508. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  1509. {
  1510. vmcs_write32(VM_ENTRY_CONTROLS, val);
  1511. vmx->vm_entry_controls_shadow = val;
  1512. }
  1513. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  1514. {
  1515. if (vmx->vm_entry_controls_shadow != val)
  1516. vm_entry_controls_init(vmx, val);
  1517. }
  1518. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  1519. {
  1520. return vmx->vm_entry_controls_shadow;
  1521. }
  1522. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1523. {
  1524. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  1525. }
  1526. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1527. {
  1528. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  1529. }
  1530. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  1531. {
  1532. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  1533. }
  1534. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  1535. {
  1536. vmcs_write32(VM_EXIT_CONTROLS, val);
  1537. vmx->vm_exit_controls_shadow = val;
  1538. }
  1539. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  1540. {
  1541. if (vmx->vm_exit_controls_shadow != val)
  1542. vm_exit_controls_init(vmx, val);
  1543. }
  1544. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  1545. {
  1546. return vmx->vm_exit_controls_shadow;
  1547. }
  1548. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1549. {
  1550. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  1551. }
  1552. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1553. {
  1554. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  1555. }
  1556. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1557. {
  1558. vmx->segment_cache.bitmask = 0;
  1559. }
  1560. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1561. unsigned field)
  1562. {
  1563. bool ret;
  1564. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1565. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1566. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1567. vmx->segment_cache.bitmask = 0;
  1568. }
  1569. ret = vmx->segment_cache.bitmask & mask;
  1570. vmx->segment_cache.bitmask |= mask;
  1571. return ret;
  1572. }
  1573. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1574. {
  1575. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1576. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1577. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1578. return *p;
  1579. }
  1580. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1581. {
  1582. ulong *p = &vmx->segment_cache.seg[seg].base;
  1583. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1584. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1585. return *p;
  1586. }
  1587. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1588. {
  1589. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1590. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1591. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1592. return *p;
  1593. }
  1594. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1595. {
  1596. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1597. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1598. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1599. return *p;
  1600. }
  1601. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1602. {
  1603. u32 eb;
  1604. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1605. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  1606. if ((vcpu->guest_debug &
  1607. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1608. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1609. eb |= 1u << BP_VECTOR;
  1610. if (to_vmx(vcpu)->rmode.vm86_active)
  1611. eb = ~0;
  1612. if (enable_ept)
  1613. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  1614. /* When we are running a nested L2 guest and L1 specified for it a
  1615. * certain exception bitmap, we must trap the same exceptions and pass
  1616. * them to L1. When running L2, we will only handle the exceptions
  1617. * specified above if L1 did not want them.
  1618. */
  1619. if (is_guest_mode(vcpu))
  1620. eb |= get_vmcs12(vcpu)->exception_bitmap;
  1621. vmcs_write32(EXCEPTION_BITMAP, eb);
  1622. }
  1623. /*
  1624. * Check if MSR is intercepted for currently loaded MSR bitmap.
  1625. */
  1626. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  1627. {
  1628. unsigned long *msr_bitmap;
  1629. int f = sizeof(unsigned long);
  1630. if (!cpu_has_vmx_msr_bitmap())
  1631. return true;
  1632. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  1633. if (msr <= 0x1fff) {
  1634. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  1635. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  1636. msr &= 0x1fff;
  1637. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  1638. }
  1639. return true;
  1640. }
  1641. /*
  1642. * Check if MSR is intercepted for L01 MSR bitmap.
  1643. */
  1644. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  1645. {
  1646. unsigned long *msr_bitmap;
  1647. int f = sizeof(unsigned long);
  1648. if (!cpu_has_vmx_msr_bitmap())
  1649. return true;
  1650. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  1651. if (msr <= 0x1fff) {
  1652. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  1653. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  1654. msr &= 0x1fff;
  1655. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  1656. }
  1657. return true;
  1658. }
  1659. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1660. unsigned long entry, unsigned long exit)
  1661. {
  1662. vm_entry_controls_clearbit(vmx, entry);
  1663. vm_exit_controls_clearbit(vmx, exit);
  1664. }
  1665. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  1666. {
  1667. unsigned i;
  1668. struct msr_autoload *m = &vmx->msr_autoload;
  1669. switch (msr) {
  1670. case MSR_EFER:
  1671. if (cpu_has_load_ia32_efer) {
  1672. clear_atomic_switch_msr_special(vmx,
  1673. VM_ENTRY_LOAD_IA32_EFER,
  1674. VM_EXIT_LOAD_IA32_EFER);
  1675. return;
  1676. }
  1677. break;
  1678. case MSR_CORE_PERF_GLOBAL_CTRL:
  1679. if (cpu_has_load_perf_global_ctrl) {
  1680. clear_atomic_switch_msr_special(vmx,
  1681. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1682. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  1683. return;
  1684. }
  1685. break;
  1686. }
  1687. for (i = 0; i < m->nr; ++i)
  1688. if (m->guest[i].index == msr)
  1689. break;
  1690. if (i == m->nr)
  1691. return;
  1692. --m->nr;
  1693. m->guest[i] = m->guest[m->nr];
  1694. m->host[i] = m->host[m->nr];
  1695. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1696. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1697. }
  1698. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1699. unsigned long entry, unsigned long exit,
  1700. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  1701. u64 guest_val, u64 host_val)
  1702. {
  1703. vmcs_write64(guest_val_vmcs, guest_val);
  1704. vmcs_write64(host_val_vmcs, host_val);
  1705. vm_entry_controls_setbit(vmx, entry);
  1706. vm_exit_controls_setbit(vmx, exit);
  1707. }
  1708. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  1709. u64 guest_val, u64 host_val)
  1710. {
  1711. unsigned i;
  1712. struct msr_autoload *m = &vmx->msr_autoload;
  1713. switch (msr) {
  1714. case MSR_EFER:
  1715. if (cpu_has_load_ia32_efer) {
  1716. add_atomic_switch_msr_special(vmx,
  1717. VM_ENTRY_LOAD_IA32_EFER,
  1718. VM_EXIT_LOAD_IA32_EFER,
  1719. GUEST_IA32_EFER,
  1720. HOST_IA32_EFER,
  1721. guest_val, host_val);
  1722. return;
  1723. }
  1724. break;
  1725. case MSR_CORE_PERF_GLOBAL_CTRL:
  1726. if (cpu_has_load_perf_global_ctrl) {
  1727. add_atomic_switch_msr_special(vmx,
  1728. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1729. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  1730. GUEST_IA32_PERF_GLOBAL_CTRL,
  1731. HOST_IA32_PERF_GLOBAL_CTRL,
  1732. guest_val, host_val);
  1733. return;
  1734. }
  1735. break;
  1736. case MSR_IA32_PEBS_ENABLE:
  1737. /* PEBS needs a quiescent period after being disabled (to write
  1738. * a record). Disabling PEBS through VMX MSR swapping doesn't
  1739. * provide that period, so a CPU could write host's record into
  1740. * guest's memory.
  1741. */
  1742. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  1743. }
  1744. for (i = 0; i < m->nr; ++i)
  1745. if (m->guest[i].index == msr)
  1746. break;
  1747. if (i == NR_AUTOLOAD_MSRS) {
  1748. printk_once(KERN_WARNING "Not enough msr switch entries. "
  1749. "Can't add msr %x\n", msr);
  1750. return;
  1751. } else if (i == m->nr) {
  1752. ++m->nr;
  1753. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1754. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1755. }
  1756. m->guest[i].index = msr;
  1757. m->guest[i].value = guest_val;
  1758. m->host[i].index = msr;
  1759. m->host[i].value = host_val;
  1760. }
  1761. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  1762. {
  1763. u64 guest_efer = vmx->vcpu.arch.efer;
  1764. u64 ignore_bits = 0;
  1765. if (!enable_ept) {
  1766. /*
  1767. * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
  1768. * host CPUID is more efficient than testing guest CPUID
  1769. * or CR4. Host SMEP is anyway a requirement for guest SMEP.
  1770. */
  1771. if (boot_cpu_has(X86_FEATURE_SMEP))
  1772. guest_efer |= EFER_NX;
  1773. else if (!(guest_efer & EFER_NX))
  1774. ignore_bits |= EFER_NX;
  1775. }
  1776. /*
  1777. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  1778. */
  1779. ignore_bits |= EFER_SCE;
  1780. #ifdef CONFIG_X86_64
  1781. ignore_bits |= EFER_LMA | EFER_LME;
  1782. /* SCE is meaningful only in long mode on Intel */
  1783. if (guest_efer & EFER_LMA)
  1784. ignore_bits &= ~(u64)EFER_SCE;
  1785. #endif
  1786. clear_atomic_switch_msr(vmx, MSR_EFER);
  1787. /*
  1788. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  1789. * On CPUs that support "load IA32_EFER", always switch EFER
  1790. * atomically, since it's faster than switching it manually.
  1791. */
  1792. if (cpu_has_load_ia32_efer ||
  1793. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  1794. if (!(guest_efer & EFER_LMA))
  1795. guest_efer &= ~EFER_LME;
  1796. if (guest_efer != host_efer)
  1797. add_atomic_switch_msr(vmx, MSR_EFER,
  1798. guest_efer, host_efer);
  1799. return false;
  1800. } else {
  1801. guest_efer &= ~ignore_bits;
  1802. guest_efer |= host_efer & ignore_bits;
  1803. vmx->guest_msrs[efer_offset].data = guest_efer;
  1804. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  1805. return true;
  1806. }
  1807. }
  1808. #ifdef CONFIG_X86_32
  1809. /*
  1810. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  1811. * VMCS rather than the segment table. KVM uses this helper to figure
  1812. * out the current bases to poke them into the VMCS before entry.
  1813. */
  1814. static unsigned long segment_base(u16 selector)
  1815. {
  1816. struct desc_struct *table;
  1817. unsigned long v;
  1818. if (!(selector & ~SEGMENT_RPL_MASK))
  1819. return 0;
  1820. table = get_current_gdt_ro();
  1821. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  1822. u16 ldt_selector = kvm_read_ldt();
  1823. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  1824. return 0;
  1825. table = (struct desc_struct *)segment_base(ldt_selector);
  1826. }
  1827. v = get_desc_base(&table[selector >> 3]);
  1828. return v;
  1829. }
  1830. #endif
  1831. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  1832. {
  1833. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1834. int i;
  1835. if (vmx->host_state.loaded)
  1836. return;
  1837. vmx->host_state.loaded = 1;
  1838. /*
  1839. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  1840. * allow segment selectors with cpl > 0 or ti == 1.
  1841. */
  1842. vmx->host_state.ldt_sel = kvm_read_ldt();
  1843. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  1844. savesegment(fs, vmx->host_state.fs_sel);
  1845. if (!(vmx->host_state.fs_sel & 7)) {
  1846. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  1847. vmx->host_state.fs_reload_needed = 0;
  1848. } else {
  1849. vmcs_write16(HOST_FS_SELECTOR, 0);
  1850. vmx->host_state.fs_reload_needed = 1;
  1851. }
  1852. savesegment(gs, vmx->host_state.gs_sel);
  1853. if (!(vmx->host_state.gs_sel & 7))
  1854. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  1855. else {
  1856. vmcs_write16(HOST_GS_SELECTOR, 0);
  1857. vmx->host_state.gs_ldt_reload_needed = 1;
  1858. }
  1859. #ifdef CONFIG_X86_64
  1860. savesegment(ds, vmx->host_state.ds_sel);
  1861. savesegment(es, vmx->host_state.es_sel);
  1862. #endif
  1863. #ifdef CONFIG_X86_64
  1864. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  1865. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  1866. #else
  1867. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  1868. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  1869. #endif
  1870. #ifdef CONFIG_X86_64
  1871. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1872. if (is_long_mode(&vmx->vcpu))
  1873. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1874. #endif
  1875. if (boot_cpu_has(X86_FEATURE_MPX))
  1876. rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1877. for (i = 0; i < vmx->save_nmsrs; ++i)
  1878. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  1879. vmx->guest_msrs[i].data,
  1880. vmx->guest_msrs[i].mask);
  1881. }
  1882. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  1883. {
  1884. if (!vmx->host_state.loaded)
  1885. return;
  1886. ++vmx->vcpu.stat.host_state_reload;
  1887. vmx->host_state.loaded = 0;
  1888. #ifdef CONFIG_X86_64
  1889. if (is_long_mode(&vmx->vcpu))
  1890. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1891. #endif
  1892. if (vmx->host_state.gs_ldt_reload_needed) {
  1893. kvm_load_ldt(vmx->host_state.ldt_sel);
  1894. #ifdef CONFIG_X86_64
  1895. load_gs_index(vmx->host_state.gs_sel);
  1896. #else
  1897. loadsegment(gs, vmx->host_state.gs_sel);
  1898. #endif
  1899. }
  1900. if (vmx->host_state.fs_reload_needed)
  1901. loadsegment(fs, vmx->host_state.fs_sel);
  1902. #ifdef CONFIG_X86_64
  1903. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  1904. loadsegment(ds, vmx->host_state.ds_sel);
  1905. loadsegment(es, vmx->host_state.es_sel);
  1906. }
  1907. #endif
  1908. invalidate_tss_limit();
  1909. #ifdef CONFIG_X86_64
  1910. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1911. #endif
  1912. if (vmx->host_state.msr_host_bndcfgs)
  1913. wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1914. load_fixmap_gdt(raw_smp_processor_id());
  1915. }
  1916. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  1917. {
  1918. preempt_disable();
  1919. __vmx_load_host_state(vmx);
  1920. preempt_enable();
  1921. }
  1922. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  1923. {
  1924. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  1925. struct pi_desc old, new;
  1926. unsigned int dest;
  1927. /*
  1928. * In case of hot-plug or hot-unplug, we may have to undo
  1929. * vmx_vcpu_pi_put even if there is no assigned device. And we
  1930. * always keep PI.NDST up to date for simplicity: it makes the
  1931. * code easier, and CPU migration is not a fast path.
  1932. */
  1933. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  1934. return;
  1935. /*
  1936. * First handle the simple case where no cmpxchg is necessary; just
  1937. * allow posting non-urgent interrupts.
  1938. *
  1939. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  1940. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  1941. * expects the VCPU to be on the blocked_vcpu_list that matches
  1942. * PI.NDST.
  1943. */
  1944. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  1945. vcpu->cpu == cpu) {
  1946. pi_clear_sn(pi_desc);
  1947. return;
  1948. }
  1949. /* The full case. */
  1950. do {
  1951. old.control = new.control = pi_desc->control;
  1952. dest = cpu_physical_id(cpu);
  1953. if (x2apic_enabled())
  1954. new.ndst = dest;
  1955. else
  1956. new.ndst = (dest << 8) & 0xFF00;
  1957. new.sn = 0;
  1958. } while (cmpxchg64(&pi_desc->control, old.control,
  1959. new.control) != old.control);
  1960. }
  1961. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  1962. {
  1963. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  1964. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  1965. }
  1966. /*
  1967. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  1968. * vcpu mutex is already taken.
  1969. */
  1970. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1971. {
  1972. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1973. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  1974. if (!already_loaded) {
  1975. loaded_vmcs_clear(vmx->loaded_vmcs);
  1976. local_irq_disable();
  1977. crash_disable_local_vmclear(cpu);
  1978. /*
  1979. * Read loaded_vmcs->cpu should be before fetching
  1980. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  1981. * See the comments in __loaded_vmcs_clear().
  1982. */
  1983. smp_rmb();
  1984. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  1985. &per_cpu(loaded_vmcss_on_cpu, cpu));
  1986. crash_enable_local_vmclear(cpu);
  1987. local_irq_enable();
  1988. }
  1989. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  1990. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  1991. vmcs_load(vmx->loaded_vmcs->vmcs);
  1992. indirect_branch_prediction_barrier();
  1993. }
  1994. if (!already_loaded) {
  1995. void *gdt = get_current_gdt_ro();
  1996. unsigned long sysenter_esp;
  1997. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1998. /*
  1999. * Linux uses per-cpu TSS and GDT, so set these when switching
  2000. * processors. See 22.2.4.
  2001. */
  2002. vmcs_writel(HOST_TR_BASE,
  2003. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2004. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2005. /*
  2006. * VM exits change the host TR limit to 0x67 after a VM
  2007. * exit. This is okay, since 0x67 covers everything except
  2008. * the IO bitmap and have have code to handle the IO bitmap
  2009. * being lost after a VM exit.
  2010. */
  2011. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2012. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2013. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2014. vmx->loaded_vmcs->cpu = cpu;
  2015. }
  2016. /* Setup TSC multiplier */
  2017. if (kvm_has_tsc_control &&
  2018. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2019. decache_tsc_multiplier(vmx);
  2020. vmx_vcpu_pi_load(vcpu, cpu);
  2021. vmx->host_pkru = read_pkru();
  2022. vmx->host_debugctlmsr = get_debugctlmsr();
  2023. }
  2024. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2025. {
  2026. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2027. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2028. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2029. !kvm_vcpu_apicv_active(vcpu))
  2030. return;
  2031. /* Set SN when the vCPU is preempted */
  2032. if (vcpu->preempted)
  2033. pi_set_sn(pi_desc);
  2034. }
  2035. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2036. {
  2037. vmx_vcpu_pi_put(vcpu);
  2038. __vmx_load_host_state(to_vmx(vcpu));
  2039. }
  2040. static bool emulation_required(struct kvm_vcpu *vcpu)
  2041. {
  2042. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2043. }
  2044. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2045. /*
  2046. * Return the cr0 value that a nested guest would read. This is a combination
  2047. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2048. * its hypervisor (cr0_read_shadow).
  2049. */
  2050. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2051. {
  2052. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2053. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2054. }
  2055. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2056. {
  2057. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2058. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2059. }
  2060. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2061. {
  2062. unsigned long rflags, save_rflags;
  2063. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2064. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2065. rflags = vmcs_readl(GUEST_RFLAGS);
  2066. if (to_vmx(vcpu)->rmode.vm86_active) {
  2067. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2068. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2069. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2070. }
  2071. to_vmx(vcpu)->rflags = rflags;
  2072. }
  2073. return to_vmx(vcpu)->rflags;
  2074. }
  2075. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2076. {
  2077. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2078. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2079. to_vmx(vcpu)->rflags = rflags;
  2080. if (to_vmx(vcpu)->rmode.vm86_active) {
  2081. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2082. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2083. }
  2084. vmcs_writel(GUEST_RFLAGS, rflags);
  2085. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2086. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2087. }
  2088. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2089. {
  2090. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2091. int ret = 0;
  2092. if (interruptibility & GUEST_INTR_STATE_STI)
  2093. ret |= KVM_X86_SHADOW_INT_STI;
  2094. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2095. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2096. return ret;
  2097. }
  2098. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2099. {
  2100. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2101. u32 interruptibility = interruptibility_old;
  2102. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2103. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2104. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2105. else if (mask & KVM_X86_SHADOW_INT_STI)
  2106. interruptibility |= GUEST_INTR_STATE_STI;
  2107. if ((interruptibility != interruptibility_old))
  2108. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2109. }
  2110. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2111. {
  2112. unsigned long rip;
  2113. rip = kvm_rip_read(vcpu);
  2114. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2115. kvm_rip_write(vcpu, rip);
  2116. /* skipping an emulated instruction also counts */
  2117. vmx_set_interrupt_shadow(vcpu, 0);
  2118. }
  2119. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2120. unsigned long exit_qual)
  2121. {
  2122. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2123. unsigned int nr = vcpu->arch.exception.nr;
  2124. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2125. if (vcpu->arch.exception.has_error_code) {
  2126. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2127. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2128. }
  2129. if (kvm_exception_is_soft(nr))
  2130. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2131. else
  2132. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2133. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2134. vmx_get_nmi_mask(vcpu))
  2135. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2136. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2137. }
  2138. /*
  2139. * KVM wants to inject page-faults which it got to the guest. This function
  2140. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2141. */
  2142. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2143. {
  2144. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2145. unsigned int nr = vcpu->arch.exception.nr;
  2146. if (nr == PF_VECTOR) {
  2147. if (vcpu->arch.exception.nested_apf) {
  2148. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2149. return 1;
  2150. }
  2151. /*
  2152. * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
  2153. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  2154. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
  2155. * can be written only when inject_pending_event runs. This should be
  2156. * conditional on a new capability---if the capability is disabled,
  2157. * kvm_multiple_exception would write the ancillary information to
  2158. * CR2 or DR6, for backwards ABI-compatibility.
  2159. */
  2160. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2161. vcpu->arch.exception.error_code)) {
  2162. *exit_qual = vcpu->arch.cr2;
  2163. return 1;
  2164. }
  2165. } else {
  2166. if (vmcs12->exception_bitmap & (1u << nr)) {
  2167. if (nr == DB_VECTOR)
  2168. *exit_qual = vcpu->arch.dr6;
  2169. else
  2170. *exit_qual = 0;
  2171. return 1;
  2172. }
  2173. }
  2174. return 0;
  2175. }
  2176. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2177. {
  2178. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2179. unsigned nr = vcpu->arch.exception.nr;
  2180. bool has_error_code = vcpu->arch.exception.has_error_code;
  2181. u32 error_code = vcpu->arch.exception.error_code;
  2182. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2183. if (has_error_code) {
  2184. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2185. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2186. }
  2187. if (vmx->rmode.vm86_active) {
  2188. int inc_eip = 0;
  2189. if (kvm_exception_is_soft(nr))
  2190. inc_eip = vcpu->arch.event_exit_inst_len;
  2191. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2192. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2193. return;
  2194. }
  2195. if (kvm_exception_is_soft(nr)) {
  2196. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2197. vmx->vcpu.arch.event_exit_inst_len);
  2198. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2199. } else
  2200. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2201. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2202. }
  2203. static bool vmx_rdtscp_supported(void)
  2204. {
  2205. return cpu_has_vmx_rdtscp();
  2206. }
  2207. static bool vmx_invpcid_supported(void)
  2208. {
  2209. return cpu_has_vmx_invpcid() && enable_ept;
  2210. }
  2211. /*
  2212. * Swap MSR entry in host/guest MSR entry array.
  2213. */
  2214. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2215. {
  2216. struct shared_msr_entry tmp;
  2217. tmp = vmx->guest_msrs[to];
  2218. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2219. vmx->guest_msrs[from] = tmp;
  2220. }
  2221. /*
  2222. * Set up the vmcs to automatically save and restore system
  2223. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2224. * mode, as fiddling with msrs is very expensive.
  2225. */
  2226. static void setup_msrs(struct vcpu_vmx *vmx)
  2227. {
  2228. int save_nmsrs, index;
  2229. save_nmsrs = 0;
  2230. #ifdef CONFIG_X86_64
  2231. if (is_long_mode(&vmx->vcpu)) {
  2232. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2233. if (index >= 0)
  2234. move_msr_up(vmx, index, save_nmsrs++);
  2235. index = __find_msr_index(vmx, MSR_LSTAR);
  2236. if (index >= 0)
  2237. move_msr_up(vmx, index, save_nmsrs++);
  2238. index = __find_msr_index(vmx, MSR_CSTAR);
  2239. if (index >= 0)
  2240. move_msr_up(vmx, index, save_nmsrs++);
  2241. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2242. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2243. move_msr_up(vmx, index, save_nmsrs++);
  2244. /*
  2245. * MSR_STAR is only needed on long mode guests, and only
  2246. * if efer.sce is enabled.
  2247. */
  2248. index = __find_msr_index(vmx, MSR_STAR);
  2249. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2250. move_msr_up(vmx, index, save_nmsrs++);
  2251. }
  2252. #endif
  2253. index = __find_msr_index(vmx, MSR_EFER);
  2254. if (index >= 0 && update_transition_efer(vmx, index))
  2255. move_msr_up(vmx, index, save_nmsrs++);
  2256. vmx->save_nmsrs = save_nmsrs;
  2257. if (cpu_has_vmx_msr_bitmap())
  2258. vmx_update_msr_bitmap(&vmx->vcpu);
  2259. }
  2260. /*
  2261. * reads and returns guest's timestamp counter "register"
  2262. * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
  2263. * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
  2264. */
  2265. static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
  2266. {
  2267. u64 host_tsc, tsc_offset;
  2268. host_tsc = rdtsc();
  2269. tsc_offset = vmcs_read64(TSC_OFFSET);
  2270. return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
  2271. }
  2272. /*
  2273. * writes 'offset' into guest's timestamp counter offset register
  2274. */
  2275. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2276. {
  2277. if (is_guest_mode(vcpu)) {
  2278. /*
  2279. * We're here if L1 chose not to trap WRMSR to TSC. According
  2280. * to the spec, this should set L1's TSC; The offset that L1
  2281. * set for L2 remains unchanged, and still needs to be added
  2282. * to the newly set TSC to get L2's TSC.
  2283. */
  2284. struct vmcs12 *vmcs12;
  2285. /* recalculate vmcs02.TSC_OFFSET: */
  2286. vmcs12 = get_vmcs12(vcpu);
  2287. vmcs_write64(TSC_OFFSET, offset +
  2288. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  2289. vmcs12->tsc_offset : 0));
  2290. } else {
  2291. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2292. vmcs_read64(TSC_OFFSET), offset);
  2293. vmcs_write64(TSC_OFFSET, offset);
  2294. }
  2295. }
  2296. /*
  2297. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2298. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2299. * all guests if the "nested" module option is off, and can also be disabled
  2300. * for a single guest by disabling its VMX cpuid bit.
  2301. */
  2302. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2303. {
  2304. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2305. }
  2306. /*
  2307. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2308. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2309. * The same values should also be used to verify that vmcs12 control fields are
  2310. * valid during nested entry from L1 to L2.
  2311. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2312. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2313. * bit in the high half is on if the corresponding bit in the control field
  2314. * may be on. See also vmx_control_verify().
  2315. */
  2316. static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
  2317. {
  2318. /*
  2319. * Note that as a general rule, the high half of the MSRs (bits in
  2320. * the control fields which may be 1) should be initialized by the
  2321. * intersection of the underlying hardware's MSR (i.e., features which
  2322. * can be supported) and the list of features we want to expose -
  2323. * because they are known to be properly supported in our code.
  2324. * Also, usually, the low half of the MSRs (bits which must be 1) can
  2325. * be set to 0, meaning that L1 may turn off any of these bits. The
  2326. * reason is that if one of these bits is necessary, it will appear
  2327. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  2328. * fields of vmcs01 and vmcs02, will turn these bits off - and
  2329. * nested_vmx_exit_reflected() will not pass related exits to L1.
  2330. * These rules have exceptions below.
  2331. */
  2332. /* pin-based controls */
  2333. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2334. vmx->nested.nested_vmx_pinbased_ctls_low,
  2335. vmx->nested.nested_vmx_pinbased_ctls_high);
  2336. vmx->nested.nested_vmx_pinbased_ctls_low |=
  2337. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2338. vmx->nested.nested_vmx_pinbased_ctls_high &=
  2339. PIN_BASED_EXT_INTR_MASK |
  2340. PIN_BASED_NMI_EXITING |
  2341. PIN_BASED_VIRTUAL_NMIS;
  2342. vmx->nested.nested_vmx_pinbased_ctls_high |=
  2343. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2344. PIN_BASED_VMX_PREEMPTION_TIMER;
  2345. if (kvm_vcpu_apicv_active(&vmx->vcpu))
  2346. vmx->nested.nested_vmx_pinbased_ctls_high |=
  2347. PIN_BASED_POSTED_INTR;
  2348. /* exit controls */
  2349. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  2350. vmx->nested.nested_vmx_exit_ctls_low,
  2351. vmx->nested.nested_vmx_exit_ctls_high);
  2352. vmx->nested.nested_vmx_exit_ctls_low =
  2353. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2354. vmx->nested.nested_vmx_exit_ctls_high &=
  2355. #ifdef CONFIG_X86_64
  2356. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  2357. #endif
  2358. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  2359. vmx->nested.nested_vmx_exit_ctls_high |=
  2360. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  2361. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  2362. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  2363. if (kvm_mpx_supported())
  2364. vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  2365. /* We support free control of debug control saving. */
  2366. vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  2367. /* entry controls */
  2368. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  2369. vmx->nested.nested_vmx_entry_ctls_low,
  2370. vmx->nested.nested_vmx_entry_ctls_high);
  2371. vmx->nested.nested_vmx_entry_ctls_low =
  2372. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2373. vmx->nested.nested_vmx_entry_ctls_high &=
  2374. #ifdef CONFIG_X86_64
  2375. VM_ENTRY_IA32E_MODE |
  2376. #endif
  2377. VM_ENTRY_LOAD_IA32_PAT;
  2378. vmx->nested.nested_vmx_entry_ctls_high |=
  2379. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  2380. if (kvm_mpx_supported())
  2381. vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  2382. /* We support free control of debug control loading. */
  2383. vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2384. /* cpu-based controls */
  2385. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  2386. vmx->nested.nested_vmx_procbased_ctls_low,
  2387. vmx->nested.nested_vmx_procbased_ctls_high);
  2388. vmx->nested.nested_vmx_procbased_ctls_low =
  2389. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2390. vmx->nested.nested_vmx_procbased_ctls_high &=
  2391. CPU_BASED_VIRTUAL_INTR_PENDING |
  2392. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  2393. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  2394. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  2395. CPU_BASED_CR3_STORE_EXITING |
  2396. #ifdef CONFIG_X86_64
  2397. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  2398. #endif
  2399. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  2400. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  2401. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  2402. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  2403. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2404. /*
  2405. * We can allow some features even when not supported by the
  2406. * hardware. For example, L1 can specify an MSR bitmap - and we
  2407. * can use it to avoid exits to L1 - even when L0 runs L2
  2408. * without MSR bitmaps.
  2409. */
  2410. vmx->nested.nested_vmx_procbased_ctls_high |=
  2411. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2412. CPU_BASED_USE_MSR_BITMAPS;
  2413. /* We support free control of CR3 access interception. */
  2414. vmx->nested.nested_vmx_procbased_ctls_low &=
  2415. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  2416. /*
  2417. * secondary cpu-based controls. Do not include those that
  2418. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  2419. */
  2420. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  2421. vmx->nested.nested_vmx_secondary_ctls_low,
  2422. vmx->nested.nested_vmx_secondary_ctls_high);
  2423. vmx->nested.nested_vmx_secondary_ctls_low = 0;
  2424. vmx->nested.nested_vmx_secondary_ctls_high &=
  2425. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2426. SECONDARY_EXEC_DESC |
  2427. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2428. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2429. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2430. SECONDARY_EXEC_WBINVD_EXITING;
  2431. if (enable_ept) {
  2432. /* nested EPT: emulate EPT also to L1 */
  2433. vmx->nested.nested_vmx_secondary_ctls_high |=
  2434. SECONDARY_EXEC_ENABLE_EPT;
  2435. vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  2436. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  2437. if (cpu_has_vmx_ept_execute_only())
  2438. vmx->nested.nested_vmx_ept_caps |=
  2439. VMX_EPT_EXECUTE_ONLY_BIT;
  2440. vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
  2441. vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  2442. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  2443. VMX_EPT_1GB_PAGE_BIT;
  2444. if (enable_ept_ad_bits) {
  2445. vmx->nested.nested_vmx_secondary_ctls_high |=
  2446. SECONDARY_EXEC_ENABLE_PML;
  2447. vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
  2448. }
  2449. }
  2450. if (cpu_has_vmx_vmfunc()) {
  2451. vmx->nested.nested_vmx_secondary_ctls_high |=
  2452. SECONDARY_EXEC_ENABLE_VMFUNC;
  2453. /*
  2454. * Advertise EPTP switching unconditionally
  2455. * since we emulate it
  2456. */
  2457. if (enable_ept)
  2458. vmx->nested.nested_vmx_vmfunc_controls =
  2459. VMX_VMFUNC_EPTP_SWITCHING;
  2460. }
  2461. /*
  2462. * Old versions of KVM use the single-context version without
  2463. * checking for support, so declare that it is supported even
  2464. * though it is treated as global context. The alternative is
  2465. * not failing the single-context invvpid, and it is worse.
  2466. */
  2467. if (enable_vpid) {
  2468. vmx->nested.nested_vmx_secondary_ctls_high |=
  2469. SECONDARY_EXEC_ENABLE_VPID;
  2470. vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
  2471. VMX_VPID_EXTENT_SUPPORTED_MASK;
  2472. }
  2473. if (enable_unrestricted_guest)
  2474. vmx->nested.nested_vmx_secondary_ctls_high |=
  2475. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2476. /* miscellaneous data */
  2477. rdmsr(MSR_IA32_VMX_MISC,
  2478. vmx->nested.nested_vmx_misc_low,
  2479. vmx->nested.nested_vmx_misc_high);
  2480. vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
  2481. vmx->nested.nested_vmx_misc_low |=
  2482. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  2483. VMX_MISC_ACTIVITY_HLT;
  2484. vmx->nested.nested_vmx_misc_high = 0;
  2485. /*
  2486. * This MSR reports some information about VMX support. We
  2487. * should return information about the VMX we emulate for the
  2488. * guest, and the VMCS structure we give it - not about the
  2489. * VMX support of the underlying hardware.
  2490. */
  2491. vmx->nested.nested_vmx_basic =
  2492. VMCS12_REVISION |
  2493. VMX_BASIC_TRUE_CTLS |
  2494. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  2495. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  2496. if (cpu_has_vmx_basic_inout())
  2497. vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
  2498. /*
  2499. * These MSRs specify bits which the guest must keep fixed on
  2500. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  2501. * We picked the standard core2 setting.
  2502. */
  2503. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  2504. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  2505. vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
  2506. vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
  2507. /* These MSRs specify bits which the guest must keep fixed off. */
  2508. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
  2509. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
  2510. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  2511. vmx->nested.nested_vmx_vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  2512. }
  2513. /*
  2514. * if fixed0[i] == 1: val[i] must be 1
  2515. * if fixed1[i] == 0: val[i] must be 0
  2516. */
  2517. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  2518. {
  2519. return ((val & fixed1) | fixed0) == val;
  2520. }
  2521. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  2522. {
  2523. return fixed_bits_valid(control, low, high);
  2524. }
  2525. static inline u64 vmx_control_msr(u32 low, u32 high)
  2526. {
  2527. return low | ((u64)high << 32);
  2528. }
  2529. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  2530. {
  2531. superset &= mask;
  2532. subset &= mask;
  2533. return (superset | subset) == superset;
  2534. }
  2535. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  2536. {
  2537. const u64 feature_and_reserved =
  2538. /* feature (except bit 48; see below) */
  2539. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  2540. /* reserved */
  2541. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  2542. u64 vmx_basic = vmx->nested.nested_vmx_basic;
  2543. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  2544. return -EINVAL;
  2545. /*
  2546. * KVM does not emulate a version of VMX that constrains physical
  2547. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  2548. */
  2549. if (data & BIT_ULL(48))
  2550. return -EINVAL;
  2551. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  2552. vmx_basic_vmcs_revision_id(data))
  2553. return -EINVAL;
  2554. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  2555. return -EINVAL;
  2556. vmx->nested.nested_vmx_basic = data;
  2557. return 0;
  2558. }
  2559. static int
  2560. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  2561. {
  2562. u64 supported;
  2563. u32 *lowp, *highp;
  2564. switch (msr_index) {
  2565. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2566. lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
  2567. highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
  2568. break;
  2569. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2570. lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
  2571. highp = &vmx->nested.nested_vmx_procbased_ctls_high;
  2572. break;
  2573. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2574. lowp = &vmx->nested.nested_vmx_exit_ctls_low;
  2575. highp = &vmx->nested.nested_vmx_exit_ctls_high;
  2576. break;
  2577. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2578. lowp = &vmx->nested.nested_vmx_entry_ctls_low;
  2579. highp = &vmx->nested.nested_vmx_entry_ctls_high;
  2580. break;
  2581. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2582. lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
  2583. highp = &vmx->nested.nested_vmx_secondary_ctls_high;
  2584. break;
  2585. default:
  2586. BUG();
  2587. }
  2588. supported = vmx_control_msr(*lowp, *highp);
  2589. /* Check must-be-1 bits are still 1. */
  2590. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  2591. return -EINVAL;
  2592. /* Check must-be-0 bits are still 0. */
  2593. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  2594. return -EINVAL;
  2595. *lowp = data;
  2596. *highp = data >> 32;
  2597. return 0;
  2598. }
  2599. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  2600. {
  2601. const u64 feature_and_reserved_bits =
  2602. /* feature */
  2603. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  2604. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  2605. /* reserved */
  2606. GENMASK_ULL(13, 9) | BIT_ULL(31);
  2607. u64 vmx_misc;
  2608. vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
  2609. vmx->nested.nested_vmx_misc_high);
  2610. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  2611. return -EINVAL;
  2612. if ((vmx->nested.nested_vmx_pinbased_ctls_high &
  2613. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  2614. vmx_misc_preemption_timer_rate(data) !=
  2615. vmx_misc_preemption_timer_rate(vmx_misc))
  2616. return -EINVAL;
  2617. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  2618. return -EINVAL;
  2619. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  2620. return -EINVAL;
  2621. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  2622. return -EINVAL;
  2623. vmx->nested.nested_vmx_misc_low = data;
  2624. vmx->nested.nested_vmx_misc_high = data >> 32;
  2625. return 0;
  2626. }
  2627. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  2628. {
  2629. u64 vmx_ept_vpid_cap;
  2630. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
  2631. vmx->nested.nested_vmx_vpid_caps);
  2632. /* Every bit is either reserved or a feature bit. */
  2633. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  2634. return -EINVAL;
  2635. vmx->nested.nested_vmx_ept_caps = data;
  2636. vmx->nested.nested_vmx_vpid_caps = data >> 32;
  2637. return 0;
  2638. }
  2639. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  2640. {
  2641. u64 *msr;
  2642. switch (msr_index) {
  2643. case MSR_IA32_VMX_CR0_FIXED0:
  2644. msr = &vmx->nested.nested_vmx_cr0_fixed0;
  2645. break;
  2646. case MSR_IA32_VMX_CR4_FIXED0:
  2647. msr = &vmx->nested.nested_vmx_cr4_fixed0;
  2648. break;
  2649. default:
  2650. BUG();
  2651. }
  2652. /*
  2653. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  2654. * must be 1 in the restored value.
  2655. */
  2656. if (!is_bitwise_subset(data, *msr, -1ULL))
  2657. return -EINVAL;
  2658. *msr = data;
  2659. return 0;
  2660. }
  2661. /*
  2662. * Called when userspace is restoring VMX MSRs.
  2663. *
  2664. * Returns 0 on success, non-0 otherwise.
  2665. */
  2666. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  2667. {
  2668. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2669. switch (msr_index) {
  2670. case MSR_IA32_VMX_BASIC:
  2671. return vmx_restore_vmx_basic(vmx, data);
  2672. case MSR_IA32_VMX_PINBASED_CTLS:
  2673. case MSR_IA32_VMX_PROCBASED_CTLS:
  2674. case MSR_IA32_VMX_EXIT_CTLS:
  2675. case MSR_IA32_VMX_ENTRY_CTLS:
  2676. /*
  2677. * The "non-true" VMX capability MSRs are generated from the
  2678. * "true" MSRs, so we do not support restoring them directly.
  2679. *
  2680. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  2681. * should restore the "true" MSRs with the must-be-1 bits
  2682. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  2683. * DEFAULT SETTINGS".
  2684. */
  2685. return -EINVAL;
  2686. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2687. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2688. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2689. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2690. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2691. return vmx_restore_control_msr(vmx, msr_index, data);
  2692. case MSR_IA32_VMX_MISC:
  2693. return vmx_restore_vmx_misc(vmx, data);
  2694. case MSR_IA32_VMX_CR0_FIXED0:
  2695. case MSR_IA32_VMX_CR4_FIXED0:
  2696. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  2697. case MSR_IA32_VMX_CR0_FIXED1:
  2698. case MSR_IA32_VMX_CR4_FIXED1:
  2699. /*
  2700. * These MSRs are generated based on the vCPU's CPUID, so we
  2701. * do not support restoring them directly.
  2702. */
  2703. return -EINVAL;
  2704. case MSR_IA32_VMX_EPT_VPID_CAP:
  2705. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  2706. case MSR_IA32_VMX_VMCS_ENUM:
  2707. vmx->nested.nested_vmx_vmcs_enum = data;
  2708. return 0;
  2709. default:
  2710. /*
  2711. * The rest of the VMX capability MSRs do not support restore.
  2712. */
  2713. return -EINVAL;
  2714. }
  2715. }
  2716. /* Returns 0 on success, non-0 otherwise. */
  2717. static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  2718. {
  2719. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2720. switch (msr_index) {
  2721. case MSR_IA32_VMX_BASIC:
  2722. *pdata = vmx->nested.nested_vmx_basic;
  2723. break;
  2724. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2725. case MSR_IA32_VMX_PINBASED_CTLS:
  2726. *pdata = vmx_control_msr(
  2727. vmx->nested.nested_vmx_pinbased_ctls_low,
  2728. vmx->nested.nested_vmx_pinbased_ctls_high);
  2729. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  2730. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2731. break;
  2732. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2733. case MSR_IA32_VMX_PROCBASED_CTLS:
  2734. *pdata = vmx_control_msr(
  2735. vmx->nested.nested_vmx_procbased_ctls_low,
  2736. vmx->nested.nested_vmx_procbased_ctls_high);
  2737. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  2738. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2739. break;
  2740. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2741. case MSR_IA32_VMX_EXIT_CTLS:
  2742. *pdata = vmx_control_msr(
  2743. vmx->nested.nested_vmx_exit_ctls_low,
  2744. vmx->nested.nested_vmx_exit_ctls_high);
  2745. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  2746. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2747. break;
  2748. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2749. case MSR_IA32_VMX_ENTRY_CTLS:
  2750. *pdata = vmx_control_msr(
  2751. vmx->nested.nested_vmx_entry_ctls_low,
  2752. vmx->nested.nested_vmx_entry_ctls_high);
  2753. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  2754. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2755. break;
  2756. case MSR_IA32_VMX_MISC:
  2757. *pdata = vmx_control_msr(
  2758. vmx->nested.nested_vmx_misc_low,
  2759. vmx->nested.nested_vmx_misc_high);
  2760. break;
  2761. case MSR_IA32_VMX_CR0_FIXED0:
  2762. *pdata = vmx->nested.nested_vmx_cr0_fixed0;
  2763. break;
  2764. case MSR_IA32_VMX_CR0_FIXED1:
  2765. *pdata = vmx->nested.nested_vmx_cr0_fixed1;
  2766. break;
  2767. case MSR_IA32_VMX_CR4_FIXED0:
  2768. *pdata = vmx->nested.nested_vmx_cr4_fixed0;
  2769. break;
  2770. case MSR_IA32_VMX_CR4_FIXED1:
  2771. *pdata = vmx->nested.nested_vmx_cr4_fixed1;
  2772. break;
  2773. case MSR_IA32_VMX_VMCS_ENUM:
  2774. *pdata = vmx->nested.nested_vmx_vmcs_enum;
  2775. break;
  2776. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2777. *pdata = vmx_control_msr(
  2778. vmx->nested.nested_vmx_secondary_ctls_low,
  2779. vmx->nested.nested_vmx_secondary_ctls_high);
  2780. break;
  2781. case MSR_IA32_VMX_EPT_VPID_CAP:
  2782. *pdata = vmx->nested.nested_vmx_ept_caps |
  2783. ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
  2784. break;
  2785. case MSR_IA32_VMX_VMFUNC:
  2786. *pdata = vmx->nested.nested_vmx_vmfunc_controls;
  2787. break;
  2788. default:
  2789. return 1;
  2790. }
  2791. return 0;
  2792. }
  2793. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  2794. uint64_t val)
  2795. {
  2796. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  2797. return !(val & ~valid_bits);
  2798. }
  2799. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  2800. {
  2801. return 1;
  2802. }
  2803. /*
  2804. * Reads an msr value (of 'msr_index') into 'pdata'.
  2805. * Returns 0 on success, non-0 otherwise.
  2806. * Assumes vcpu_load() was already called.
  2807. */
  2808. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2809. {
  2810. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2811. struct shared_msr_entry *msr;
  2812. switch (msr_info->index) {
  2813. #ifdef CONFIG_X86_64
  2814. case MSR_FS_BASE:
  2815. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  2816. break;
  2817. case MSR_GS_BASE:
  2818. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  2819. break;
  2820. case MSR_KERNEL_GS_BASE:
  2821. vmx_load_host_state(vmx);
  2822. msr_info->data = vmx->msr_guest_kernel_gs_base;
  2823. break;
  2824. #endif
  2825. case MSR_EFER:
  2826. return kvm_get_msr_common(vcpu, msr_info);
  2827. case MSR_IA32_TSC:
  2828. msr_info->data = guest_read_tsc(vcpu);
  2829. break;
  2830. case MSR_IA32_SPEC_CTRL:
  2831. if (!msr_info->host_initiated &&
  2832. !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
  2833. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  2834. return 1;
  2835. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  2836. break;
  2837. case MSR_IA32_ARCH_CAPABILITIES:
  2838. if (!msr_info->host_initiated &&
  2839. !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
  2840. return 1;
  2841. msr_info->data = to_vmx(vcpu)->arch_capabilities;
  2842. break;
  2843. case MSR_IA32_SYSENTER_CS:
  2844. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  2845. break;
  2846. case MSR_IA32_SYSENTER_EIP:
  2847. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  2848. break;
  2849. case MSR_IA32_SYSENTER_ESP:
  2850. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  2851. break;
  2852. case MSR_IA32_BNDCFGS:
  2853. if (!kvm_mpx_supported() ||
  2854. (!msr_info->host_initiated &&
  2855. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  2856. return 1;
  2857. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  2858. break;
  2859. case MSR_IA32_MCG_EXT_CTL:
  2860. if (!msr_info->host_initiated &&
  2861. !(vmx->msr_ia32_feature_control &
  2862. FEATURE_CONTROL_LMCE))
  2863. return 1;
  2864. msr_info->data = vcpu->arch.mcg_ext_ctl;
  2865. break;
  2866. case MSR_IA32_FEATURE_CONTROL:
  2867. msr_info->data = vmx->msr_ia32_feature_control;
  2868. break;
  2869. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  2870. if (!nested_vmx_allowed(vcpu))
  2871. return 1;
  2872. return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
  2873. case MSR_IA32_XSS:
  2874. if (!vmx_xsaves_supported())
  2875. return 1;
  2876. msr_info->data = vcpu->arch.ia32_xss;
  2877. break;
  2878. case MSR_TSC_AUX:
  2879. if (!msr_info->host_initiated &&
  2880. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  2881. return 1;
  2882. /* Otherwise falls through */
  2883. default:
  2884. msr = find_msr_entry(vmx, msr_info->index);
  2885. if (msr) {
  2886. msr_info->data = msr->data;
  2887. break;
  2888. }
  2889. return kvm_get_msr_common(vcpu, msr_info);
  2890. }
  2891. return 0;
  2892. }
  2893. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  2894. /*
  2895. * Writes msr value into into the appropriate "register".
  2896. * Returns 0 on success, non-0 otherwise.
  2897. * Assumes vcpu_load() was already called.
  2898. */
  2899. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2900. {
  2901. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2902. struct shared_msr_entry *msr;
  2903. int ret = 0;
  2904. u32 msr_index = msr_info->index;
  2905. u64 data = msr_info->data;
  2906. switch (msr_index) {
  2907. case MSR_EFER:
  2908. ret = kvm_set_msr_common(vcpu, msr_info);
  2909. break;
  2910. #ifdef CONFIG_X86_64
  2911. case MSR_FS_BASE:
  2912. vmx_segment_cache_clear(vmx);
  2913. vmcs_writel(GUEST_FS_BASE, data);
  2914. break;
  2915. case MSR_GS_BASE:
  2916. vmx_segment_cache_clear(vmx);
  2917. vmcs_writel(GUEST_GS_BASE, data);
  2918. break;
  2919. case MSR_KERNEL_GS_BASE:
  2920. vmx_load_host_state(vmx);
  2921. vmx->msr_guest_kernel_gs_base = data;
  2922. break;
  2923. #endif
  2924. case MSR_IA32_SYSENTER_CS:
  2925. vmcs_write32(GUEST_SYSENTER_CS, data);
  2926. break;
  2927. case MSR_IA32_SYSENTER_EIP:
  2928. vmcs_writel(GUEST_SYSENTER_EIP, data);
  2929. break;
  2930. case MSR_IA32_SYSENTER_ESP:
  2931. vmcs_writel(GUEST_SYSENTER_ESP, data);
  2932. break;
  2933. case MSR_IA32_BNDCFGS:
  2934. if (!kvm_mpx_supported() ||
  2935. (!msr_info->host_initiated &&
  2936. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  2937. return 1;
  2938. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  2939. (data & MSR_IA32_BNDCFGS_RSVD))
  2940. return 1;
  2941. vmcs_write64(GUEST_BNDCFGS, data);
  2942. break;
  2943. case MSR_IA32_TSC:
  2944. kvm_write_tsc(vcpu, msr_info);
  2945. break;
  2946. case MSR_IA32_SPEC_CTRL:
  2947. if (!msr_info->host_initiated &&
  2948. !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
  2949. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  2950. return 1;
  2951. /* The STIBP bit doesn't fault even if it's not advertised */
  2952. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
  2953. return 1;
  2954. vmx->spec_ctrl = data;
  2955. if (!data)
  2956. break;
  2957. /*
  2958. * For non-nested:
  2959. * When it's written (to non-zero) for the first time, pass
  2960. * it through.
  2961. *
  2962. * For nested:
  2963. * The handling of the MSR bitmap for L2 guests is done in
  2964. * nested_vmx_merge_msr_bitmap. We should not touch the
  2965. * vmcs02.msr_bitmap here since it gets completely overwritten
  2966. * in the merging. We update the vmcs01 here for L1 as well
  2967. * since it will end up touching the MSR anyway now.
  2968. */
  2969. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  2970. MSR_IA32_SPEC_CTRL,
  2971. MSR_TYPE_RW);
  2972. break;
  2973. case MSR_IA32_PRED_CMD:
  2974. if (!msr_info->host_initiated &&
  2975. !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
  2976. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  2977. return 1;
  2978. if (data & ~PRED_CMD_IBPB)
  2979. return 1;
  2980. if (!data)
  2981. break;
  2982. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  2983. /*
  2984. * For non-nested:
  2985. * When it's written (to non-zero) for the first time, pass
  2986. * it through.
  2987. *
  2988. * For nested:
  2989. * The handling of the MSR bitmap for L2 guests is done in
  2990. * nested_vmx_merge_msr_bitmap. We should not touch the
  2991. * vmcs02.msr_bitmap here since it gets completely overwritten
  2992. * in the merging.
  2993. */
  2994. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  2995. MSR_TYPE_W);
  2996. break;
  2997. case MSR_IA32_ARCH_CAPABILITIES:
  2998. if (!msr_info->host_initiated)
  2999. return 1;
  3000. vmx->arch_capabilities = data;
  3001. break;
  3002. case MSR_IA32_CR_PAT:
  3003. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3004. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3005. return 1;
  3006. vmcs_write64(GUEST_IA32_PAT, data);
  3007. vcpu->arch.pat = data;
  3008. break;
  3009. }
  3010. ret = kvm_set_msr_common(vcpu, msr_info);
  3011. break;
  3012. case MSR_IA32_TSC_ADJUST:
  3013. ret = kvm_set_msr_common(vcpu, msr_info);
  3014. break;
  3015. case MSR_IA32_MCG_EXT_CTL:
  3016. if ((!msr_info->host_initiated &&
  3017. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3018. FEATURE_CONTROL_LMCE)) ||
  3019. (data & ~MCG_EXT_CTL_LMCE_EN))
  3020. return 1;
  3021. vcpu->arch.mcg_ext_ctl = data;
  3022. break;
  3023. case MSR_IA32_FEATURE_CONTROL:
  3024. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3025. (to_vmx(vcpu)->msr_ia32_feature_control &
  3026. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3027. return 1;
  3028. vmx->msr_ia32_feature_control = data;
  3029. if (msr_info->host_initiated && data == 0)
  3030. vmx_leave_nested(vcpu);
  3031. break;
  3032. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3033. if (!msr_info->host_initiated)
  3034. return 1; /* they are read-only */
  3035. if (!nested_vmx_allowed(vcpu))
  3036. return 1;
  3037. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3038. case MSR_IA32_XSS:
  3039. if (!vmx_xsaves_supported())
  3040. return 1;
  3041. /*
  3042. * The only supported bit as of Skylake is bit 8, but
  3043. * it is not supported on KVM.
  3044. */
  3045. if (data != 0)
  3046. return 1;
  3047. vcpu->arch.ia32_xss = data;
  3048. if (vcpu->arch.ia32_xss != host_xss)
  3049. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3050. vcpu->arch.ia32_xss, host_xss);
  3051. else
  3052. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3053. break;
  3054. case MSR_TSC_AUX:
  3055. if (!msr_info->host_initiated &&
  3056. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3057. return 1;
  3058. /* Check reserved bit, higher 32 bits should be zero */
  3059. if ((data >> 32) != 0)
  3060. return 1;
  3061. /* Otherwise falls through */
  3062. default:
  3063. msr = find_msr_entry(vmx, msr_index);
  3064. if (msr) {
  3065. u64 old_msr_data = msr->data;
  3066. msr->data = data;
  3067. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3068. preempt_disable();
  3069. ret = kvm_set_shared_msr(msr->index, msr->data,
  3070. msr->mask);
  3071. preempt_enable();
  3072. if (ret)
  3073. msr->data = old_msr_data;
  3074. }
  3075. break;
  3076. }
  3077. ret = kvm_set_msr_common(vcpu, msr_info);
  3078. }
  3079. return ret;
  3080. }
  3081. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3082. {
  3083. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3084. switch (reg) {
  3085. case VCPU_REGS_RSP:
  3086. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3087. break;
  3088. case VCPU_REGS_RIP:
  3089. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3090. break;
  3091. case VCPU_EXREG_PDPTR:
  3092. if (enable_ept)
  3093. ept_save_pdptrs(vcpu);
  3094. break;
  3095. default:
  3096. break;
  3097. }
  3098. }
  3099. static __init int cpu_has_kvm_support(void)
  3100. {
  3101. return cpu_has_vmx();
  3102. }
  3103. static __init int vmx_disabled_by_bios(void)
  3104. {
  3105. u64 msr;
  3106. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3107. if (msr & FEATURE_CONTROL_LOCKED) {
  3108. /* launched w/ TXT and VMX disabled */
  3109. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3110. && tboot_enabled())
  3111. return 1;
  3112. /* launched w/o TXT and VMX only enabled w/ TXT */
  3113. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3114. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3115. && !tboot_enabled()) {
  3116. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3117. "activate TXT before enabling KVM\n");
  3118. return 1;
  3119. }
  3120. /* launched w/o TXT and VMX disabled */
  3121. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3122. && !tboot_enabled())
  3123. return 1;
  3124. }
  3125. return 0;
  3126. }
  3127. static void kvm_cpu_vmxon(u64 addr)
  3128. {
  3129. cr4_set_bits(X86_CR4_VMXE);
  3130. intel_pt_handle_vmx(1);
  3131. asm volatile (ASM_VMX_VMXON_RAX
  3132. : : "a"(&addr), "m"(addr)
  3133. : "memory", "cc");
  3134. }
  3135. static int hardware_enable(void)
  3136. {
  3137. int cpu = raw_smp_processor_id();
  3138. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3139. u64 old, test_bits;
  3140. if (cr4_read_shadow() & X86_CR4_VMXE)
  3141. return -EBUSY;
  3142. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3143. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3144. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3145. /*
  3146. * Now we can enable the vmclear operation in kdump
  3147. * since the loaded_vmcss_on_cpu list on this cpu
  3148. * has been initialized.
  3149. *
  3150. * Though the cpu is not in VMX operation now, there
  3151. * is no problem to enable the vmclear operation
  3152. * for the loaded_vmcss_on_cpu list is empty!
  3153. */
  3154. crash_enable_local_vmclear(cpu);
  3155. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3156. test_bits = FEATURE_CONTROL_LOCKED;
  3157. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3158. if (tboot_enabled())
  3159. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3160. if ((old & test_bits) != test_bits) {
  3161. /* enable and lock */
  3162. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3163. }
  3164. kvm_cpu_vmxon(phys_addr);
  3165. if (enable_ept)
  3166. ept_sync_global();
  3167. return 0;
  3168. }
  3169. static void vmclear_local_loaded_vmcss(void)
  3170. {
  3171. int cpu = raw_smp_processor_id();
  3172. struct loaded_vmcs *v, *n;
  3173. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3174. loaded_vmcss_on_cpu_link)
  3175. __loaded_vmcs_clear(v);
  3176. }
  3177. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3178. * tricks.
  3179. */
  3180. static void kvm_cpu_vmxoff(void)
  3181. {
  3182. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  3183. intel_pt_handle_vmx(0);
  3184. cr4_clear_bits(X86_CR4_VMXE);
  3185. }
  3186. static void hardware_disable(void)
  3187. {
  3188. vmclear_local_loaded_vmcss();
  3189. kvm_cpu_vmxoff();
  3190. }
  3191. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3192. u32 msr, u32 *result)
  3193. {
  3194. u32 vmx_msr_low, vmx_msr_high;
  3195. u32 ctl = ctl_min | ctl_opt;
  3196. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3197. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3198. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3199. /* Ensure minimum (required) set of control bits are supported. */
  3200. if (ctl_min & ~ctl)
  3201. return -EIO;
  3202. *result = ctl;
  3203. return 0;
  3204. }
  3205. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3206. {
  3207. u32 vmx_msr_low, vmx_msr_high;
  3208. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3209. return vmx_msr_high & ctl;
  3210. }
  3211. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3212. {
  3213. u32 vmx_msr_low, vmx_msr_high;
  3214. u32 min, opt, min2, opt2;
  3215. u32 _pin_based_exec_control = 0;
  3216. u32 _cpu_based_exec_control = 0;
  3217. u32 _cpu_based_2nd_exec_control = 0;
  3218. u32 _vmexit_control = 0;
  3219. u32 _vmentry_control = 0;
  3220. min = CPU_BASED_HLT_EXITING |
  3221. #ifdef CONFIG_X86_64
  3222. CPU_BASED_CR8_LOAD_EXITING |
  3223. CPU_BASED_CR8_STORE_EXITING |
  3224. #endif
  3225. CPU_BASED_CR3_LOAD_EXITING |
  3226. CPU_BASED_CR3_STORE_EXITING |
  3227. CPU_BASED_UNCOND_IO_EXITING |
  3228. CPU_BASED_MOV_DR_EXITING |
  3229. CPU_BASED_USE_TSC_OFFSETING |
  3230. CPU_BASED_INVLPG_EXITING |
  3231. CPU_BASED_RDPMC_EXITING;
  3232. if (!kvm_mwait_in_guest())
  3233. min |= CPU_BASED_MWAIT_EXITING |
  3234. CPU_BASED_MONITOR_EXITING;
  3235. opt = CPU_BASED_TPR_SHADOW |
  3236. CPU_BASED_USE_MSR_BITMAPS |
  3237. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3238. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3239. &_cpu_based_exec_control) < 0)
  3240. return -EIO;
  3241. #ifdef CONFIG_X86_64
  3242. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3243. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3244. ~CPU_BASED_CR8_STORE_EXITING;
  3245. #endif
  3246. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3247. min2 = 0;
  3248. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3249. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3250. SECONDARY_EXEC_WBINVD_EXITING |
  3251. SECONDARY_EXEC_ENABLE_VPID |
  3252. SECONDARY_EXEC_ENABLE_EPT |
  3253. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3254. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3255. SECONDARY_EXEC_DESC |
  3256. SECONDARY_EXEC_RDTSCP |
  3257. SECONDARY_EXEC_ENABLE_INVPCID |
  3258. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3259. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3260. SECONDARY_EXEC_SHADOW_VMCS |
  3261. SECONDARY_EXEC_XSAVES |
  3262. SECONDARY_EXEC_RDSEED_EXITING |
  3263. SECONDARY_EXEC_RDRAND_EXITING |
  3264. SECONDARY_EXEC_ENABLE_PML |
  3265. SECONDARY_EXEC_TSC_SCALING |
  3266. SECONDARY_EXEC_ENABLE_VMFUNC;
  3267. if (adjust_vmx_controls(min2, opt2,
  3268. MSR_IA32_VMX_PROCBASED_CTLS2,
  3269. &_cpu_based_2nd_exec_control) < 0)
  3270. return -EIO;
  3271. }
  3272. #ifndef CONFIG_X86_64
  3273. if (!(_cpu_based_2nd_exec_control &
  3274. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3275. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3276. #endif
  3277. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3278. _cpu_based_2nd_exec_control &= ~(
  3279. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3280. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3281. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3282. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3283. &vmx_capability.ept, &vmx_capability.vpid);
  3284. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  3285. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  3286. enabled */
  3287. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  3288. CPU_BASED_CR3_STORE_EXITING |
  3289. CPU_BASED_INVLPG_EXITING);
  3290. } else if (vmx_capability.ept) {
  3291. vmx_capability.ept = 0;
  3292. pr_warn_once("EPT CAP should not exist if not support "
  3293. "1-setting enable EPT VM-execution control\n");
  3294. }
  3295. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  3296. vmx_capability.vpid) {
  3297. vmx_capability.vpid = 0;
  3298. pr_warn_once("VPID CAP should not exist if not support "
  3299. "1-setting enable VPID VM-execution control\n");
  3300. }
  3301. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  3302. #ifdef CONFIG_X86_64
  3303. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  3304. #endif
  3305. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  3306. VM_EXIT_CLEAR_BNDCFGS;
  3307. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  3308. &_vmexit_control) < 0)
  3309. return -EIO;
  3310. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  3311. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  3312. PIN_BASED_VMX_PREEMPTION_TIMER;
  3313. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  3314. &_pin_based_exec_control) < 0)
  3315. return -EIO;
  3316. if (cpu_has_broken_vmx_preemption_timer())
  3317. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  3318. if (!(_cpu_based_2nd_exec_control &
  3319. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  3320. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  3321. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3322. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  3323. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  3324. &_vmentry_control) < 0)
  3325. return -EIO;
  3326. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  3327. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  3328. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  3329. return -EIO;
  3330. #ifdef CONFIG_X86_64
  3331. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  3332. if (vmx_msr_high & (1u<<16))
  3333. return -EIO;
  3334. #endif
  3335. /* Require Write-Back (WB) memory type for VMCS accesses. */
  3336. if (((vmx_msr_high >> 18) & 15) != 6)
  3337. return -EIO;
  3338. vmcs_conf->size = vmx_msr_high & 0x1fff;
  3339. vmcs_conf->order = get_order(vmcs_conf->size);
  3340. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  3341. vmcs_conf->revision_id = vmx_msr_low;
  3342. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  3343. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  3344. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  3345. vmcs_conf->vmexit_ctrl = _vmexit_control;
  3346. vmcs_conf->vmentry_ctrl = _vmentry_control;
  3347. cpu_has_load_ia32_efer =
  3348. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3349. VM_ENTRY_LOAD_IA32_EFER)
  3350. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3351. VM_EXIT_LOAD_IA32_EFER);
  3352. cpu_has_load_perf_global_ctrl =
  3353. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  3354. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  3355. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  3356. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  3357. /*
  3358. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  3359. * but due to errata below it can't be used. Workaround is to use
  3360. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  3361. *
  3362. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  3363. *
  3364. * AAK155 (model 26)
  3365. * AAP115 (model 30)
  3366. * AAT100 (model 37)
  3367. * BC86,AAY89,BD102 (model 44)
  3368. * BA97 (model 46)
  3369. *
  3370. */
  3371. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  3372. switch (boot_cpu_data.x86_model) {
  3373. case 26:
  3374. case 30:
  3375. case 37:
  3376. case 44:
  3377. case 46:
  3378. cpu_has_load_perf_global_ctrl = false;
  3379. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  3380. "does not work properly. Using workaround\n");
  3381. break;
  3382. default:
  3383. break;
  3384. }
  3385. }
  3386. if (boot_cpu_has(X86_FEATURE_XSAVES))
  3387. rdmsrl(MSR_IA32_XSS, host_xss);
  3388. return 0;
  3389. }
  3390. static struct vmcs *alloc_vmcs_cpu(int cpu)
  3391. {
  3392. int node = cpu_to_node(cpu);
  3393. struct page *pages;
  3394. struct vmcs *vmcs;
  3395. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  3396. if (!pages)
  3397. return NULL;
  3398. vmcs = page_address(pages);
  3399. memset(vmcs, 0, vmcs_config.size);
  3400. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  3401. return vmcs;
  3402. }
  3403. static void free_vmcs(struct vmcs *vmcs)
  3404. {
  3405. free_pages((unsigned long)vmcs, vmcs_config.order);
  3406. }
  3407. /*
  3408. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  3409. */
  3410. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3411. {
  3412. if (!loaded_vmcs->vmcs)
  3413. return;
  3414. loaded_vmcs_clear(loaded_vmcs);
  3415. free_vmcs(loaded_vmcs->vmcs);
  3416. loaded_vmcs->vmcs = NULL;
  3417. if (loaded_vmcs->msr_bitmap)
  3418. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  3419. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  3420. }
  3421. static struct vmcs *alloc_vmcs(void)
  3422. {
  3423. return alloc_vmcs_cpu(raw_smp_processor_id());
  3424. }
  3425. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  3426. {
  3427. loaded_vmcs->vmcs = alloc_vmcs();
  3428. if (!loaded_vmcs->vmcs)
  3429. return -ENOMEM;
  3430. loaded_vmcs->shadow_vmcs = NULL;
  3431. loaded_vmcs_init(loaded_vmcs);
  3432. if (cpu_has_vmx_msr_bitmap()) {
  3433. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  3434. if (!loaded_vmcs->msr_bitmap)
  3435. goto out_vmcs;
  3436. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  3437. }
  3438. return 0;
  3439. out_vmcs:
  3440. free_loaded_vmcs(loaded_vmcs);
  3441. return -ENOMEM;
  3442. }
  3443. static void free_kvm_area(void)
  3444. {
  3445. int cpu;
  3446. for_each_possible_cpu(cpu) {
  3447. free_vmcs(per_cpu(vmxarea, cpu));
  3448. per_cpu(vmxarea, cpu) = NULL;
  3449. }
  3450. }
  3451. enum vmcs_field_width {
  3452. VMCS_FIELD_WIDTH_U16 = 0,
  3453. VMCS_FIELD_WIDTH_U64 = 1,
  3454. VMCS_FIELD_WIDTH_U32 = 2,
  3455. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  3456. };
  3457. static inline int vmcs_field_width(unsigned long field)
  3458. {
  3459. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  3460. return VMCS_FIELD_WIDTH_U32;
  3461. return (field >> 13) & 0x3 ;
  3462. }
  3463. static inline int vmcs_field_readonly(unsigned long field)
  3464. {
  3465. return (((field >> 10) & 0x3) == 1);
  3466. }
  3467. static void init_vmcs_shadow_fields(void)
  3468. {
  3469. int i, j;
  3470. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  3471. u16 field = shadow_read_only_fields[i];
  3472. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3473. (i + 1 == max_shadow_read_only_fields ||
  3474. shadow_read_only_fields[i + 1] != field + 1))
  3475. pr_err("Missing field from shadow_read_only_field %x\n",
  3476. field + 1);
  3477. clear_bit(field, vmx_vmread_bitmap);
  3478. #ifdef CONFIG_X86_64
  3479. if (field & 1)
  3480. continue;
  3481. #endif
  3482. if (j < i)
  3483. shadow_read_only_fields[j] = field;
  3484. j++;
  3485. }
  3486. max_shadow_read_only_fields = j;
  3487. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  3488. u16 field = shadow_read_write_fields[i];
  3489. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  3490. (i + 1 == max_shadow_read_write_fields ||
  3491. shadow_read_write_fields[i + 1] != field + 1))
  3492. pr_err("Missing field from shadow_read_write_field %x\n",
  3493. field + 1);
  3494. /*
  3495. * PML and the preemption timer can be emulated, but the
  3496. * processor cannot vmwrite to fields that don't exist
  3497. * on bare metal.
  3498. */
  3499. switch (field) {
  3500. case GUEST_PML_INDEX:
  3501. if (!cpu_has_vmx_pml())
  3502. continue;
  3503. break;
  3504. case VMX_PREEMPTION_TIMER_VALUE:
  3505. if (!cpu_has_vmx_preemption_timer())
  3506. continue;
  3507. break;
  3508. case GUEST_INTR_STATUS:
  3509. if (!cpu_has_vmx_apicv())
  3510. continue;
  3511. break;
  3512. default:
  3513. break;
  3514. }
  3515. clear_bit(field, vmx_vmwrite_bitmap);
  3516. clear_bit(field, vmx_vmread_bitmap);
  3517. #ifdef CONFIG_X86_64
  3518. if (field & 1)
  3519. continue;
  3520. #endif
  3521. if (j < i)
  3522. shadow_read_write_fields[j] = field;
  3523. j++;
  3524. }
  3525. max_shadow_read_write_fields = j;
  3526. }
  3527. static __init int alloc_kvm_area(void)
  3528. {
  3529. int cpu;
  3530. for_each_possible_cpu(cpu) {
  3531. struct vmcs *vmcs;
  3532. vmcs = alloc_vmcs_cpu(cpu);
  3533. if (!vmcs) {
  3534. free_kvm_area();
  3535. return -ENOMEM;
  3536. }
  3537. per_cpu(vmxarea, cpu) = vmcs;
  3538. }
  3539. return 0;
  3540. }
  3541. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  3542. struct kvm_segment *save)
  3543. {
  3544. if (!emulate_invalid_guest_state) {
  3545. /*
  3546. * CS and SS RPL should be equal during guest entry according
  3547. * to VMX spec, but in reality it is not always so. Since vcpu
  3548. * is in the middle of the transition from real mode to
  3549. * protected mode it is safe to assume that RPL 0 is a good
  3550. * default value.
  3551. */
  3552. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  3553. save->selector &= ~SEGMENT_RPL_MASK;
  3554. save->dpl = save->selector & SEGMENT_RPL_MASK;
  3555. save->s = 1;
  3556. }
  3557. vmx_set_segment(vcpu, save, seg);
  3558. }
  3559. static void enter_pmode(struct kvm_vcpu *vcpu)
  3560. {
  3561. unsigned long flags;
  3562. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3563. /*
  3564. * Update real mode segment cache. It may be not up-to-date if sement
  3565. * register was written while vcpu was in a guest mode.
  3566. */
  3567. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  3568. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  3569. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  3570. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  3571. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  3572. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  3573. vmx->rmode.vm86_active = 0;
  3574. vmx_segment_cache_clear(vmx);
  3575. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  3576. flags = vmcs_readl(GUEST_RFLAGS);
  3577. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  3578. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  3579. vmcs_writel(GUEST_RFLAGS, flags);
  3580. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  3581. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  3582. update_exception_bitmap(vcpu);
  3583. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  3584. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  3585. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  3586. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  3587. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  3588. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  3589. }
  3590. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  3591. {
  3592. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3593. struct kvm_segment var = *save;
  3594. var.dpl = 0x3;
  3595. if (seg == VCPU_SREG_CS)
  3596. var.type = 0x3;
  3597. if (!emulate_invalid_guest_state) {
  3598. var.selector = var.base >> 4;
  3599. var.base = var.base & 0xffff0;
  3600. var.limit = 0xffff;
  3601. var.g = 0;
  3602. var.db = 0;
  3603. var.present = 1;
  3604. var.s = 1;
  3605. var.l = 0;
  3606. var.unusable = 0;
  3607. var.type = 0x3;
  3608. var.avl = 0;
  3609. if (save->base & 0xf)
  3610. printk_once(KERN_WARNING "kvm: segment base is not "
  3611. "paragraph aligned when entering "
  3612. "protected mode (seg=%d)", seg);
  3613. }
  3614. vmcs_write16(sf->selector, var.selector);
  3615. vmcs_writel(sf->base, var.base);
  3616. vmcs_write32(sf->limit, var.limit);
  3617. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  3618. }
  3619. static void enter_rmode(struct kvm_vcpu *vcpu)
  3620. {
  3621. unsigned long flags;
  3622. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3623. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  3624. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  3625. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  3626. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  3627. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  3628. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  3629. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  3630. vmx->rmode.vm86_active = 1;
  3631. /*
  3632. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  3633. * vcpu. Warn the user that an update is overdue.
  3634. */
  3635. if (!vcpu->kvm->arch.tss_addr)
  3636. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  3637. "called before entering vcpu\n");
  3638. vmx_segment_cache_clear(vmx);
  3639. vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
  3640. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  3641. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  3642. flags = vmcs_readl(GUEST_RFLAGS);
  3643. vmx->rmode.save_rflags = flags;
  3644. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  3645. vmcs_writel(GUEST_RFLAGS, flags);
  3646. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  3647. update_exception_bitmap(vcpu);
  3648. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  3649. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  3650. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  3651. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  3652. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  3653. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  3654. kvm_mmu_reset_context(vcpu);
  3655. }
  3656. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  3657. {
  3658. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3659. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  3660. if (!msr)
  3661. return;
  3662. /*
  3663. * Force kernel_gs_base reloading before EFER changes, as control
  3664. * of this msr depends on is_long_mode().
  3665. */
  3666. vmx_load_host_state(to_vmx(vcpu));
  3667. vcpu->arch.efer = efer;
  3668. if (efer & EFER_LMA) {
  3669. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  3670. msr->data = efer;
  3671. } else {
  3672. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  3673. msr->data = efer & ~EFER_LME;
  3674. }
  3675. setup_msrs(vmx);
  3676. }
  3677. #ifdef CONFIG_X86_64
  3678. static void enter_lmode(struct kvm_vcpu *vcpu)
  3679. {
  3680. u32 guest_tr_ar;
  3681. vmx_segment_cache_clear(to_vmx(vcpu));
  3682. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  3683. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  3684. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  3685. __func__);
  3686. vmcs_write32(GUEST_TR_AR_BYTES,
  3687. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  3688. | VMX_AR_TYPE_BUSY_64_TSS);
  3689. }
  3690. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  3691. }
  3692. static void exit_lmode(struct kvm_vcpu *vcpu)
  3693. {
  3694. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  3695. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  3696. }
  3697. #endif
  3698. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  3699. bool invalidate_gpa)
  3700. {
  3701. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  3702. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  3703. return;
  3704. ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
  3705. } else {
  3706. vpid_sync_context(vpid);
  3707. }
  3708. }
  3709. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  3710. {
  3711. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  3712. }
  3713. static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
  3714. {
  3715. if (enable_ept)
  3716. vmx_flush_tlb(vcpu, true);
  3717. }
  3718. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  3719. {
  3720. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  3721. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  3722. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  3723. }
  3724. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  3725. {
  3726. if (enable_ept && is_paging(vcpu))
  3727. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  3728. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  3729. }
  3730. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  3731. {
  3732. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  3733. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  3734. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  3735. }
  3736. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  3737. {
  3738. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  3739. if (!test_bit(VCPU_EXREG_PDPTR,
  3740. (unsigned long *)&vcpu->arch.regs_dirty))
  3741. return;
  3742. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  3743. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  3744. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  3745. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  3746. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  3747. }
  3748. }
  3749. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  3750. {
  3751. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  3752. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  3753. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  3754. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  3755. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  3756. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  3757. }
  3758. __set_bit(VCPU_EXREG_PDPTR,
  3759. (unsigned long *)&vcpu->arch.regs_avail);
  3760. __set_bit(VCPU_EXREG_PDPTR,
  3761. (unsigned long *)&vcpu->arch.regs_dirty);
  3762. }
  3763. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  3764. {
  3765. u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
  3766. u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
  3767. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  3768. if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
  3769. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  3770. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  3771. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  3772. return fixed_bits_valid(val, fixed0, fixed1);
  3773. }
  3774. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  3775. {
  3776. u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
  3777. u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
  3778. return fixed_bits_valid(val, fixed0, fixed1);
  3779. }
  3780. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  3781. {
  3782. u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
  3783. u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
  3784. return fixed_bits_valid(val, fixed0, fixed1);
  3785. }
  3786. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  3787. #define nested_guest_cr4_valid nested_cr4_valid
  3788. #define nested_host_cr4_valid nested_cr4_valid
  3789. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  3790. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  3791. unsigned long cr0,
  3792. struct kvm_vcpu *vcpu)
  3793. {
  3794. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  3795. vmx_decache_cr3(vcpu);
  3796. if (!(cr0 & X86_CR0_PG)) {
  3797. /* From paging/starting to nonpaging */
  3798. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3799. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  3800. (CPU_BASED_CR3_LOAD_EXITING |
  3801. CPU_BASED_CR3_STORE_EXITING));
  3802. vcpu->arch.cr0 = cr0;
  3803. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3804. } else if (!is_paging(vcpu)) {
  3805. /* From nonpaging to paging */
  3806. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3807. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  3808. ~(CPU_BASED_CR3_LOAD_EXITING |
  3809. CPU_BASED_CR3_STORE_EXITING));
  3810. vcpu->arch.cr0 = cr0;
  3811. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3812. }
  3813. if (!(cr0 & X86_CR0_WP))
  3814. *hw_cr0 &= ~X86_CR0_WP;
  3815. }
  3816. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  3817. {
  3818. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3819. unsigned long hw_cr0;
  3820. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  3821. if (enable_unrestricted_guest)
  3822. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  3823. else {
  3824. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  3825. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  3826. enter_pmode(vcpu);
  3827. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  3828. enter_rmode(vcpu);
  3829. }
  3830. #ifdef CONFIG_X86_64
  3831. if (vcpu->arch.efer & EFER_LME) {
  3832. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  3833. enter_lmode(vcpu);
  3834. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  3835. exit_lmode(vcpu);
  3836. }
  3837. #endif
  3838. if (enable_ept)
  3839. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  3840. vmcs_writel(CR0_READ_SHADOW, cr0);
  3841. vmcs_writel(GUEST_CR0, hw_cr0);
  3842. vcpu->arch.cr0 = cr0;
  3843. /* depends on vcpu->arch.cr0 to be set to a new value */
  3844. vmx->emulation_required = emulation_required(vcpu);
  3845. }
  3846. static int get_ept_level(struct kvm_vcpu *vcpu)
  3847. {
  3848. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  3849. return 5;
  3850. return 4;
  3851. }
  3852. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  3853. {
  3854. u64 eptp = VMX_EPTP_MT_WB;
  3855. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  3856. if (enable_ept_ad_bits &&
  3857. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  3858. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  3859. eptp |= (root_hpa & PAGE_MASK);
  3860. return eptp;
  3861. }
  3862. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  3863. {
  3864. unsigned long guest_cr3;
  3865. u64 eptp;
  3866. guest_cr3 = cr3;
  3867. if (enable_ept) {
  3868. eptp = construct_eptp(vcpu, cr3);
  3869. vmcs_write64(EPT_POINTER, eptp);
  3870. if (is_paging(vcpu) || is_guest_mode(vcpu))
  3871. guest_cr3 = kvm_read_cr3(vcpu);
  3872. else
  3873. guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
  3874. ept_load_pdptrs(vcpu);
  3875. }
  3876. vmx_flush_tlb(vcpu, true);
  3877. vmcs_writel(GUEST_CR3, guest_cr3);
  3878. }
  3879. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  3880. {
  3881. /*
  3882. * Pass through host's Machine Check Enable value to hw_cr4, which
  3883. * is in force while we are in guest mode. Do not let guests control
  3884. * this bit, even if host CR4.MCE == 0.
  3885. */
  3886. unsigned long hw_cr4 =
  3887. (cr4_read_shadow() & X86_CR4_MCE) |
  3888. (cr4 & ~X86_CR4_MCE) |
  3889. (to_vmx(vcpu)->rmode.vm86_active ?
  3890. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  3891. if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
  3892. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  3893. SECONDARY_EXEC_DESC);
  3894. hw_cr4 &= ~X86_CR4_UMIP;
  3895. } else if (!is_guest_mode(vcpu) ||
  3896. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  3897. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  3898. SECONDARY_EXEC_DESC);
  3899. if (cr4 & X86_CR4_VMXE) {
  3900. /*
  3901. * To use VMXON (and later other VMX instructions), a guest
  3902. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  3903. * So basically the check on whether to allow nested VMX
  3904. * is here.
  3905. */
  3906. if (!nested_vmx_allowed(vcpu))
  3907. return 1;
  3908. }
  3909. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  3910. return 1;
  3911. vcpu->arch.cr4 = cr4;
  3912. if (enable_ept) {
  3913. if (!is_paging(vcpu)) {
  3914. hw_cr4 &= ~X86_CR4_PAE;
  3915. hw_cr4 |= X86_CR4_PSE;
  3916. } else if (!(cr4 & X86_CR4_PAE)) {
  3917. hw_cr4 &= ~X86_CR4_PAE;
  3918. }
  3919. }
  3920. if (!enable_unrestricted_guest && !is_paging(vcpu))
  3921. /*
  3922. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  3923. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  3924. * to be manually disabled when guest switches to non-paging
  3925. * mode.
  3926. *
  3927. * If !enable_unrestricted_guest, the CPU is always running
  3928. * with CR0.PG=1 and CR4 needs to be modified.
  3929. * If enable_unrestricted_guest, the CPU automatically
  3930. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  3931. */
  3932. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  3933. vmcs_writel(CR4_READ_SHADOW, cr4);
  3934. vmcs_writel(GUEST_CR4, hw_cr4);
  3935. return 0;
  3936. }
  3937. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  3938. struct kvm_segment *var, int seg)
  3939. {
  3940. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3941. u32 ar;
  3942. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  3943. *var = vmx->rmode.segs[seg];
  3944. if (seg == VCPU_SREG_TR
  3945. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  3946. return;
  3947. var->base = vmx_read_guest_seg_base(vmx, seg);
  3948. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3949. return;
  3950. }
  3951. var->base = vmx_read_guest_seg_base(vmx, seg);
  3952. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  3953. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3954. ar = vmx_read_guest_seg_ar(vmx, seg);
  3955. var->unusable = (ar >> 16) & 1;
  3956. var->type = ar & 15;
  3957. var->s = (ar >> 4) & 1;
  3958. var->dpl = (ar >> 5) & 3;
  3959. /*
  3960. * Some userspaces do not preserve unusable property. Since usable
  3961. * segment has to be present according to VMX spec we can use present
  3962. * property to amend userspace bug by making unusable segment always
  3963. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  3964. * segment as unusable.
  3965. */
  3966. var->present = !var->unusable;
  3967. var->avl = (ar >> 12) & 1;
  3968. var->l = (ar >> 13) & 1;
  3969. var->db = (ar >> 14) & 1;
  3970. var->g = (ar >> 15) & 1;
  3971. }
  3972. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3973. {
  3974. struct kvm_segment s;
  3975. if (to_vmx(vcpu)->rmode.vm86_active) {
  3976. vmx_get_segment(vcpu, &s, seg);
  3977. return s.base;
  3978. }
  3979. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  3980. }
  3981. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  3982. {
  3983. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3984. if (unlikely(vmx->rmode.vm86_active))
  3985. return 0;
  3986. else {
  3987. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  3988. return VMX_AR_DPL(ar);
  3989. }
  3990. }
  3991. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  3992. {
  3993. u32 ar;
  3994. if (var->unusable || !var->present)
  3995. ar = 1 << 16;
  3996. else {
  3997. ar = var->type & 15;
  3998. ar |= (var->s & 1) << 4;
  3999. ar |= (var->dpl & 3) << 5;
  4000. ar |= (var->present & 1) << 7;
  4001. ar |= (var->avl & 1) << 12;
  4002. ar |= (var->l & 1) << 13;
  4003. ar |= (var->db & 1) << 14;
  4004. ar |= (var->g & 1) << 15;
  4005. }
  4006. return ar;
  4007. }
  4008. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4009. struct kvm_segment *var, int seg)
  4010. {
  4011. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4012. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4013. vmx_segment_cache_clear(vmx);
  4014. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4015. vmx->rmode.segs[seg] = *var;
  4016. if (seg == VCPU_SREG_TR)
  4017. vmcs_write16(sf->selector, var->selector);
  4018. else if (var->s)
  4019. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4020. goto out;
  4021. }
  4022. vmcs_writel(sf->base, var->base);
  4023. vmcs_write32(sf->limit, var->limit);
  4024. vmcs_write16(sf->selector, var->selector);
  4025. /*
  4026. * Fix the "Accessed" bit in AR field of segment registers for older
  4027. * qemu binaries.
  4028. * IA32 arch specifies that at the time of processor reset the
  4029. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4030. * is setting it to 0 in the userland code. This causes invalid guest
  4031. * state vmexit when "unrestricted guest" mode is turned on.
  4032. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4033. * tree. Newer qemu binaries with that qemu fix would not need this
  4034. * kvm hack.
  4035. */
  4036. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4037. var->type |= 0x1; /* Accessed */
  4038. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4039. out:
  4040. vmx->emulation_required = emulation_required(vcpu);
  4041. }
  4042. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4043. {
  4044. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4045. *db = (ar >> 14) & 1;
  4046. *l = (ar >> 13) & 1;
  4047. }
  4048. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4049. {
  4050. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4051. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4052. }
  4053. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4054. {
  4055. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4056. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4057. }
  4058. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4059. {
  4060. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4061. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4062. }
  4063. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4064. {
  4065. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4066. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4067. }
  4068. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4069. {
  4070. struct kvm_segment var;
  4071. u32 ar;
  4072. vmx_get_segment(vcpu, &var, seg);
  4073. var.dpl = 0x3;
  4074. if (seg == VCPU_SREG_CS)
  4075. var.type = 0x3;
  4076. ar = vmx_segment_access_rights(&var);
  4077. if (var.base != (var.selector << 4))
  4078. return false;
  4079. if (var.limit != 0xffff)
  4080. return false;
  4081. if (ar != 0xf3)
  4082. return false;
  4083. return true;
  4084. }
  4085. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4086. {
  4087. struct kvm_segment cs;
  4088. unsigned int cs_rpl;
  4089. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4090. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4091. if (cs.unusable)
  4092. return false;
  4093. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4094. return false;
  4095. if (!cs.s)
  4096. return false;
  4097. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4098. if (cs.dpl > cs_rpl)
  4099. return false;
  4100. } else {
  4101. if (cs.dpl != cs_rpl)
  4102. return false;
  4103. }
  4104. if (!cs.present)
  4105. return false;
  4106. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4107. return true;
  4108. }
  4109. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4110. {
  4111. struct kvm_segment ss;
  4112. unsigned int ss_rpl;
  4113. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4114. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4115. if (ss.unusable)
  4116. return true;
  4117. if (ss.type != 3 && ss.type != 7)
  4118. return false;
  4119. if (!ss.s)
  4120. return false;
  4121. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4122. return false;
  4123. if (!ss.present)
  4124. return false;
  4125. return true;
  4126. }
  4127. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4128. {
  4129. struct kvm_segment var;
  4130. unsigned int rpl;
  4131. vmx_get_segment(vcpu, &var, seg);
  4132. rpl = var.selector & SEGMENT_RPL_MASK;
  4133. if (var.unusable)
  4134. return true;
  4135. if (!var.s)
  4136. return false;
  4137. if (!var.present)
  4138. return false;
  4139. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4140. if (var.dpl < rpl) /* DPL < RPL */
  4141. return false;
  4142. }
  4143. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4144. * rights flags
  4145. */
  4146. return true;
  4147. }
  4148. static bool tr_valid(struct kvm_vcpu *vcpu)
  4149. {
  4150. struct kvm_segment tr;
  4151. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4152. if (tr.unusable)
  4153. return false;
  4154. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4155. return false;
  4156. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4157. return false;
  4158. if (!tr.present)
  4159. return false;
  4160. return true;
  4161. }
  4162. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4163. {
  4164. struct kvm_segment ldtr;
  4165. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4166. if (ldtr.unusable)
  4167. return true;
  4168. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4169. return false;
  4170. if (ldtr.type != 2)
  4171. return false;
  4172. if (!ldtr.present)
  4173. return false;
  4174. return true;
  4175. }
  4176. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4177. {
  4178. struct kvm_segment cs, ss;
  4179. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4180. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4181. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4182. (ss.selector & SEGMENT_RPL_MASK));
  4183. }
  4184. /*
  4185. * Check if guest state is valid. Returns true if valid, false if
  4186. * not.
  4187. * We assume that registers are always usable
  4188. */
  4189. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4190. {
  4191. if (enable_unrestricted_guest)
  4192. return true;
  4193. /* real mode guest state checks */
  4194. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4195. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4196. return false;
  4197. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4198. return false;
  4199. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4200. return false;
  4201. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4202. return false;
  4203. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4204. return false;
  4205. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4206. return false;
  4207. } else {
  4208. /* protected mode guest state checks */
  4209. if (!cs_ss_rpl_check(vcpu))
  4210. return false;
  4211. if (!code_segment_valid(vcpu))
  4212. return false;
  4213. if (!stack_segment_valid(vcpu))
  4214. return false;
  4215. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4216. return false;
  4217. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4218. return false;
  4219. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4220. return false;
  4221. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4222. return false;
  4223. if (!tr_valid(vcpu))
  4224. return false;
  4225. if (!ldtr_valid(vcpu))
  4226. return false;
  4227. }
  4228. /* TODO:
  4229. * - Add checks on RIP
  4230. * - Add checks on RFLAGS
  4231. */
  4232. return true;
  4233. }
  4234. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4235. {
  4236. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4237. }
  4238. static int init_rmode_tss(struct kvm *kvm)
  4239. {
  4240. gfn_t fn;
  4241. u16 data = 0;
  4242. int idx, r;
  4243. idx = srcu_read_lock(&kvm->srcu);
  4244. fn = kvm->arch.tss_addr >> PAGE_SHIFT;
  4245. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4246. if (r < 0)
  4247. goto out;
  4248. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  4249. r = kvm_write_guest_page(kvm, fn++, &data,
  4250. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  4251. if (r < 0)
  4252. goto out;
  4253. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  4254. if (r < 0)
  4255. goto out;
  4256. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  4257. if (r < 0)
  4258. goto out;
  4259. data = ~0;
  4260. r = kvm_write_guest_page(kvm, fn, &data,
  4261. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  4262. sizeof(u8));
  4263. out:
  4264. srcu_read_unlock(&kvm->srcu, idx);
  4265. return r;
  4266. }
  4267. static int init_rmode_identity_map(struct kvm *kvm)
  4268. {
  4269. int i, idx, r = 0;
  4270. kvm_pfn_t identity_map_pfn;
  4271. u32 tmp;
  4272. /* Protect kvm->arch.ept_identity_pagetable_done. */
  4273. mutex_lock(&kvm->slots_lock);
  4274. if (likely(kvm->arch.ept_identity_pagetable_done))
  4275. goto out2;
  4276. if (!kvm->arch.ept_identity_map_addr)
  4277. kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  4278. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  4279. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  4280. kvm->arch.ept_identity_map_addr, PAGE_SIZE);
  4281. if (r < 0)
  4282. goto out2;
  4283. idx = srcu_read_lock(&kvm->srcu);
  4284. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  4285. if (r < 0)
  4286. goto out;
  4287. /* Set up identity-mapping pagetable for EPT in real mode */
  4288. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  4289. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  4290. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  4291. r = kvm_write_guest_page(kvm, identity_map_pfn,
  4292. &tmp, i * sizeof(tmp), sizeof(tmp));
  4293. if (r < 0)
  4294. goto out;
  4295. }
  4296. kvm->arch.ept_identity_pagetable_done = true;
  4297. out:
  4298. srcu_read_unlock(&kvm->srcu, idx);
  4299. out2:
  4300. mutex_unlock(&kvm->slots_lock);
  4301. return r;
  4302. }
  4303. static void seg_setup(int seg)
  4304. {
  4305. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4306. unsigned int ar;
  4307. vmcs_write16(sf->selector, 0);
  4308. vmcs_writel(sf->base, 0);
  4309. vmcs_write32(sf->limit, 0xffff);
  4310. ar = 0x93;
  4311. if (seg == VCPU_SREG_CS)
  4312. ar |= 0x08; /* code segment */
  4313. vmcs_write32(sf->ar_bytes, ar);
  4314. }
  4315. static int alloc_apic_access_page(struct kvm *kvm)
  4316. {
  4317. struct page *page;
  4318. int r = 0;
  4319. mutex_lock(&kvm->slots_lock);
  4320. if (kvm->arch.apic_access_page_done)
  4321. goto out;
  4322. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  4323. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  4324. if (r)
  4325. goto out;
  4326. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  4327. if (is_error_page(page)) {
  4328. r = -EFAULT;
  4329. goto out;
  4330. }
  4331. /*
  4332. * Do not pin the page in memory, so that memory hot-unplug
  4333. * is able to migrate it.
  4334. */
  4335. put_page(page);
  4336. kvm->arch.apic_access_page_done = true;
  4337. out:
  4338. mutex_unlock(&kvm->slots_lock);
  4339. return r;
  4340. }
  4341. static int allocate_vpid(void)
  4342. {
  4343. int vpid;
  4344. if (!enable_vpid)
  4345. return 0;
  4346. spin_lock(&vmx_vpid_lock);
  4347. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  4348. if (vpid < VMX_NR_VPIDS)
  4349. __set_bit(vpid, vmx_vpid_bitmap);
  4350. else
  4351. vpid = 0;
  4352. spin_unlock(&vmx_vpid_lock);
  4353. return vpid;
  4354. }
  4355. static void free_vpid(int vpid)
  4356. {
  4357. if (!enable_vpid || vpid == 0)
  4358. return;
  4359. spin_lock(&vmx_vpid_lock);
  4360. __clear_bit(vpid, vmx_vpid_bitmap);
  4361. spin_unlock(&vmx_vpid_lock);
  4362. }
  4363. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  4364. u32 msr, int type)
  4365. {
  4366. int f = sizeof(unsigned long);
  4367. if (!cpu_has_vmx_msr_bitmap())
  4368. return;
  4369. /*
  4370. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4371. * have the write-low and read-high bitmap offsets the wrong way round.
  4372. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4373. */
  4374. if (msr <= 0x1fff) {
  4375. if (type & MSR_TYPE_R)
  4376. /* read-low */
  4377. __clear_bit(msr, msr_bitmap + 0x000 / f);
  4378. if (type & MSR_TYPE_W)
  4379. /* write-low */
  4380. __clear_bit(msr, msr_bitmap + 0x800 / f);
  4381. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4382. msr &= 0x1fff;
  4383. if (type & MSR_TYPE_R)
  4384. /* read-high */
  4385. __clear_bit(msr, msr_bitmap + 0x400 / f);
  4386. if (type & MSR_TYPE_W)
  4387. /* write-high */
  4388. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  4389. }
  4390. }
  4391. static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  4392. u32 msr, int type)
  4393. {
  4394. int f = sizeof(unsigned long);
  4395. if (!cpu_has_vmx_msr_bitmap())
  4396. return;
  4397. /*
  4398. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4399. * have the write-low and read-high bitmap offsets the wrong way round.
  4400. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4401. */
  4402. if (msr <= 0x1fff) {
  4403. if (type & MSR_TYPE_R)
  4404. /* read-low */
  4405. __set_bit(msr, msr_bitmap + 0x000 / f);
  4406. if (type & MSR_TYPE_W)
  4407. /* write-low */
  4408. __set_bit(msr, msr_bitmap + 0x800 / f);
  4409. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4410. msr &= 0x1fff;
  4411. if (type & MSR_TYPE_R)
  4412. /* read-high */
  4413. __set_bit(msr, msr_bitmap + 0x400 / f);
  4414. if (type & MSR_TYPE_W)
  4415. /* write-high */
  4416. __set_bit(msr, msr_bitmap + 0xc00 / f);
  4417. }
  4418. }
  4419. static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  4420. u32 msr, int type, bool value)
  4421. {
  4422. if (value)
  4423. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  4424. else
  4425. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  4426. }
  4427. /*
  4428. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  4429. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  4430. */
  4431. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  4432. unsigned long *msr_bitmap_nested,
  4433. u32 msr, int type)
  4434. {
  4435. int f = sizeof(unsigned long);
  4436. /*
  4437. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  4438. * have the write-low and read-high bitmap offsets the wrong way round.
  4439. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  4440. */
  4441. if (msr <= 0x1fff) {
  4442. if (type & MSR_TYPE_R &&
  4443. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  4444. /* read-low */
  4445. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  4446. if (type & MSR_TYPE_W &&
  4447. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  4448. /* write-low */
  4449. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  4450. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  4451. msr &= 0x1fff;
  4452. if (type & MSR_TYPE_R &&
  4453. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  4454. /* read-high */
  4455. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  4456. if (type & MSR_TYPE_W &&
  4457. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  4458. /* write-high */
  4459. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  4460. }
  4461. }
  4462. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  4463. {
  4464. u8 mode = 0;
  4465. if (cpu_has_secondary_exec_ctrls() &&
  4466. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  4467. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  4468. mode |= MSR_BITMAP_MODE_X2APIC;
  4469. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  4470. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  4471. }
  4472. if (is_long_mode(vcpu))
  4473. mode |= MSR_BITMAP_MODE_LM;
  4474. return mode;
  4475. }
  4476. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  4477. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  4478. u8 mode)
  4479. {
  4480. int msr;
  4481. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  4482. unsigned word = msr / BITS_PER_LONG;
  4483. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  4484. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  4485. }
  4486. if (mode & MSR_BITMAP_MODE_X2APIC) {
  4487. /*
  4488. * TPR reads and writes can be virtualized even if virtual interrupt
  4489. * delivery is not in use.
  4490. */
  4491. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  4492. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  4493. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  4494. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  4495. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  4496. }
  4497. }
  4498. }
  4499. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  4500. {
  4501. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4502. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  4503. u8 mode = vmx_msr_bitmap_mode(vcpu);
  4504. u8 changed = mode ^ vmx->msr_bitmap_mode;
  4505. if (!changed)
  4506. return;
  4507. vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
  4508. !(mode & MSR_BITMAP_MODE_LM));
  4509. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  4510. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  4511. vmx->msr_bitmap_mode = mode;
  4512. }
  4513. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  4514. {
  4515. return enable_apicv;
  4516. }
  4517. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  4518. {
  4519. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4520. gfn_t gfn;
  4521. /*
  4522. * Don't need to mark the APIC access page dirty; it is never
  4523. * written to by the CPU during APIC virtualization.
  4524. */
  4525. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  4526. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  4527. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  4528. }
  4529. if (nested_cpu_has_posted_intr(vmcs12)) {
  4530. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  4531. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  4532. }
  4533. }
  4534. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  4535. {
  4536. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4537. int max_irr;
  4538. void *vapic_page;
  4539. u16 status;
  4540. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  4541. return;
  4542. vmx->nested.pi_pending = false;
  4543. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  4544. return;
  4545. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  4546. if (max_irr != 256) {
  4547. vapic_page = kmap(vmx->nested.virtual_apic_page);
  4548. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  4549. vapic_page, &max_irr);
  4550. kunmap(vmx->nested.virtual_apic_page);
  4551. status = vmcs_read16(GUEST_INTR_STATUS);
  4552. if ((u8)max_irr > ((u8)status & 0xff)) {
  4553. status &= ~0xff;
  4554. status |= (u8)max_irr;
  4555. vmcs_write16(GUEST_INTR_STATUS, status);
  4556. }
  4557. }
  4558. nested_mark_vmcs12_pages_dirty(vcpu);
  4559. }
  4560. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  4561. bool nested)
  4562. {
  4563. #ifdef CONFIG_SMP
  4564. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  4565. if (vcpu->mode == IN_GUEST_MODE) {
  4566. /*
  4567. * The vector of interrupt to be delivered to vcpu had
  4568. * been set in PIR before this function.
  4569. *
  4570. * Following cases will be reached in this block, and
  4571. * we always send a notification event in all cases as
  4572. * explained below.
  4573. *
  4574. * Case 1: vcpu keeps in non-root mode. Sending a
  4575. * notification event posts the interrupt to vcpu.
  4576. *
  4577. * Case 2: vcpu exits to root mode and is still
  4578. * runnable. PIR will be synced to vIRR before the
  4579. * next vcpu entry. Sending a notification event in
  4580. * this case has no effect, as vcpu is not in root
  4581. * mode.
  4582. *
  4583. * Case 3: vcpu exits to root mode and is blocked.
  4584. * vcpu_block() has already synced PIR to vIRR and
  4585. * never blocks vcpu if vIRR is not cleared. Therefore,
  4586. * a blocked vcpu here does not wait for any requested
  4587. * interrupts in PIR, and sending a notification event
  4588. * which has no effect is safe here.
  4589. */
  4590. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  4591. return true;
  4592. }
  4593. #endif
  4594. return false;
  4595. }
  4596. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  4597. int vector)
  4598. {
  4599. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4600. if (is_guest_mode(vcpu) &&
  4601. vector == vmx->nested.posted_intr_nv) {
  4602. /*
  4603. * If a posted intr is not recognized by hardware,
  4604. * we will accomplish it in the next vmentry.
  4605. */
  4606. vmx->nested.pi_pending = true;
  4607. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4608. /* the PIR and ON have been set by L1. */
  4609. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  4610. kvm_vcpu_kick(vcpu);
  4611. return 0;
  4612. }
  4613. return -1;
  4614. }
  4615. /*
  4616. * Send interrupt to vcpu via posted interrupt way.
  4617. * 1. If target vcpu is running(non-root mode), send posted interrupt
  4618. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  4619. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  4620. * interrupt from PIR in next vmentry.
  4621. */
  4622. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  4623. {
  4624. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4625. int r;
  4626. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  4627. if (!r)
  4628. return;
  4629. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  4630. return;
  4631. /* If a previous notification has sent the IPI, nothing to do. */
  4632. if (pi_test_and_set_on(&vmx->pi_desc))
  4633. return;
  4634. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  4635. kvm_vcpu_kick(vcpu);
  4636. }
  4637. /*
  4638. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  4639. * will not change in the lifetime of the guest.
  4640. * Note that host-state that does change is set elsewhere. E.g., host-state
  4641. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  4642. */
  4643. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  4644. {
  4645. u32 low32, high32;
  4646. unsigned long tmpl;
  4647. struct desc_ptr dt;
  4648. unsigned long cr0, cr3, cr4;
  4649. cr0 = read_cr0();
  4650. WARN_ON(cr0 & X86_CR0_TS);
  4651. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  4652. /*
  4653. * Save the most likely value for this task's CR3 in the VMCS.
  4654. * We can't use __get_current_cr3_fast() because we're not atomic.
  4655. */
  4656. cr3 = __read_cr3();
  4657. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  4658. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  4659. /* Save the most likely value for this task's CR4 in the VMCS. */
  4660. cr4 = cr4_read_shadow();
  4661. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  4662. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  4663. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  4664. #ifdef CONFIG_X86_64
  4665. /*
  4666. * Load null selectors, so we can avoid reloading them in
  4667. * __vmx_load_host_state(), in case userspace uses the null selectors
  4668. * too (the expected case).
  4669. */
  4670. vmcs_write16(HOST_DS_SELECTOR, 0);
  4671. vmcs_write16(HOST_ES_SELECTOR, 0);
  4672. #else
  4673. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  4674. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  4675. #endif
  4676. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  4677. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  4678. store_idt(&dt);
  4679. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  4680. vmx->host_idt_base = dt.address;
  4681. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  4682. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  4683. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  4684. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  4685. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  4686. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  4687. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  4688. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  4689. }
  4690. }
  4691. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  4692. {
  4693. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  4694. if (enable_ept)
  4695. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  4696. if (is_guest_mode(&vmx->vcpu))
  4697. vmx->vcpu.arch.cr4_guest_owned_bits &=
  4698. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  4699. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  4700. }
  4701. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  4702. {
  4703. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  4704. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  4705. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  4706. if (!enable_vnmi)
  4707. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  4708. /* Enable the preemption timer dynamically */
  4709. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  4710. return pin_based_exec_ctrl;
  4711. }
  4712. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  4713. {
  4714. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4715. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  4716. if (cpu_has_secondary_exec_ctrls()) {
  4717. if (kvm_vcpu_apicv_active(vcpu))
  4718. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4719. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  4720. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  4721. else
  4722. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4723. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  4724. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  4725. }
  4726. if (cpu_has_vmx_msr_bitmap())
  4727. vmx_update_msr_bitmap(vcpu);
  4728. }
  4729. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  4730. {
  4731. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  4732. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  4733. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  4734. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  4735. exec_control &= ~CPU_BASED_TPR_SHADOW;
  4736. #ifdef CONFIG_X86_64
  4737. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  4738. CPU_BASED_CR8_LOAD_EXITING;
  4739. #endif
  4740. }
  4741. if (!enable_ept)
  4742. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  4743. CPU_BASED_CR3_LOAD_EXITING |
  4744. CPU_BASED_INVLPG_EXITING;
  4745. return exec_control;
  4746. }
  4747. static bool vmx_rdrand_supported(void)
  4748. {
  4749. return vmcs_config.cpu_based_2nd_exec_ctrl &
  4750. SECONDARY_EXEC_RDRAND_EXITING;
  4751. }
  4752. static bool vmx_rdseed_supported(void)
  4753. {
  4754. return vmcs_config.cpu_based_2nd_exec_ctrl &
  4755. SECONDARY_EXEC_RDSEED_EXITING;
  4756. }
  4757. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  4758. {
  4759. struct kvm_vcpu *vcpu = &vmx->vcpu;
  4760. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  4761. if (!cpu_need_virtualize_apic_accesses(vcpu))
  4762. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  4763. if (vmx->vpid == 0)
  4764. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  4765. if (!enable_ept) {
  4766. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  4767. enable_unrestricted_guest = 0;
  4768. /* Enable INVPCID for non-ept guests may cause performance regression. */
  4769. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  4770. }
  4771. if (!enable_unrestricted_guest)
  4772. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  4773. if (!ple_gap)
  4774. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  4775. if (!kvm_vcpu_apicv_active(vcpu))
  4776. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  4777. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  4778. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  4779. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  4780. * in vmx_set_cr4. */
  4781. exec_control &= ~SECONDARY_EXEC_DESC;
  4782. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  4783. (handle_vmptrld).
  4784. We can NOT enable shadow_vmcs here because we don't have yet
  4785. a current VMCS12
  4786. */
  4787. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  4788. if (!enable_pml)
  4789. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  4790. if (vmx_xsaves_supported()) {
  4791. /* Exposing XSAVES only when XSAVE is exposed */
  4792. bool xsaves_enabled =
  4793. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  4794. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  4795. if (!xsaves_enabled)
  4796. exec_control &= ~SECONDARY_EXEC_XSAVES;
  4797. if (nested) {
  4798. if (xsaves_enabled)
  4799. vmx->nested.nested_vmx_secondary_ctls_high |=
  4800. SECONDARY_EXEC_XSAVES;
  4801. else
  4802. vmx->nested.nested_vmx_secondary_ctls_high &=
  4803. ~SECONDARY_EXEC_XSAVES;
  4804. }
  4805. }
  4806. if (vmx_rdtscp_supported()) {
  4807. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  4808. if (!rdtscp_enabled)
  4809. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  4810. if (nested) {
  4811. if (rdtscp_enabled)
  4812. vmx->nested.nested_vmx_secondary_ctls_high |=
  4813. SECONDARY_EXEC_RDTSCP;
  4814. else
  4815. vmx->nested.nested_vmx_secondary_ctls_high &=
  4816. ~SECONDARY_EXEC_RDTSCP;
  4817. }
  4818. }
  4819. if (vmx_invpcid_supported()) {
  4820. /* Exposing INVPCID only when PCID is exposed */
  4821. bool invpcid_enabled =
  4822. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  4823. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  4824. if (!invpcid_enabled) {
  4825. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  4826. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  4827. }
  4828. if (nested) {
  4829. if (invpcid_enabled)
  4830. vmx->nested.nested_vmx_secondary_ctls_high |=
  4831. SECONDARY_EXEC_ENABLE_INVPCID;
  4832. else
  4833. vmx->nested.nested_vmx_secondary_ctls_high &=
  4834. ~SECONDARY_EXEC_ENABLE_INVPCID;
  4835. }
  4836. }
  4837. if (vmx_rdrand_supported()) {
  4838. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  4839. if (rdrand_enabled)
  4840. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  4841. if (nested) {
  4842. if (rdrand_enabled)
  4843. vmx->nested.nested_vmx_secondary_ctls_high |=
  4844. SECONDARY_EXEC_RDRAND_EXITING;
  4845. else
  4846. vmx->nested.nested_vmx_secondary_ctls_high &=
  4847. ~SECONDARY_EXEC_RDRAND_EXITING;
  4848. }
  4849. }
  4850. if (vmx_rdseed_supported()) {
  4851. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  4852. if (rdseed_enabled)
  4853. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  4854. if (nested) {
  4855. if (rdseed_enabled)
  4856. vmx->nested.nested_vmx_secondary_ctls_high |=
  4857. SECONDARY_EXEC_RDSEED_EXITING;
  4858. else
  4859. vmx->nested.nested_vmx_secondary_ctls_high &=
  4860. ~SECONDARY_EXEC_RDSEED_EXITING;
  4861. }
  4862. }
  4863. vmx->secondary_exec_control = exec_control;
  4864. }
  4865. static void ept_set_mmio_spte_mask(void)
  4866. {
  4867. /*
  4868. * EPT Misconfigurations can be generated if the value of bits 2:0
  4869. * of an EPT paging-structure entry is 110b (write/execute).
  4870. */
  4871. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  4872. VMX_EPT_MISCONFIG_WX_VALUE);
  4873. }
  4874. #define VMX_XSS_EXIT_BITMAP 0
  4875. /*
  4876. * Sets up the vmcs for emulated real mode.
  4877. */
  4878. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  4879. {
  4880. #ifdef CONFIG_X86_64
  4881. unsigned long a;
  4882. #endif
  4883. int i;
  4884. if (enable_shadow_vmcs) {
  4885. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  4886. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  4887. }
  4888. if (cpu_has_vmx_msr_bitmap())
  4889. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  4890. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  4891. /* Control */
  4892. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  4893. vmx->hv_deadline_tsc = -1;
  4894. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  4895. if (cpu_has_secondary_exec_ctrls()) {
  4896. vmx_compute_secondary_exec_control(vmx);
  4897. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  4898. vmx->secondary_exec_control);
  4899. }
  4900. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  4901. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  4902. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  4903. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  4904. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  4905. vmcs_write16(GUEST_INTR_STATUS, 0);
  4906. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  4907. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  4908. }
  4909. if (ple_gap) {
  4910. vmcs_write32(PLE_GAP, ple_gap);
  4911. vmx->ple_window = ple_window;
  4912. vmx->ple_window_dirty = true;
  4913. }
  4914. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  4915. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  4916. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  4917. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  4918. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  4919. vmx_set_constant_host_state(vmx);
  4920. #ifdef CONFIG_X86_64
  4921. rdmsrl(MSR_FS_BASE, a);
  4922. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  4923. rdmsrl(MSR_GS_BASE, a);
  4924. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  4925. #else
  4926. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  4927. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  4928. #endif
  4929. if (cpu_has_vmx_vmfunc())
  4930. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  4931. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  4932. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  4933. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  4934. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  4935. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  4936. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  4937. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  4938. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  4939. u32 index = vmx_msr_index[i];
  4940. u32 data_low, data_high;
  4941. int j = vmx->nmsrs;
  4942. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  4943. continue;
  4944. if (wrmsr_safe(index, data_low, data_high) < 0)
  4945. continue;
  4946. vmx->guest_msrs[j].index = i;
  4947. vmx->guest_msrs[j].data = 0;
  4948. vmx->guest_msrs[j].mask = -1ull;
  4949. ++vmx->nmsrs;
  4950. }
  4951. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
  4952. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
  4953. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  4954. /* 22.2.1, 20.8.1 */
  4955. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  4956. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  4957. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  4958. set_cr4_guest_host_mask(vmx);
  4959. if (vmx_xsaves_supported())
  4960. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  4961. if (enable_pml) {
  4962. ASSERT(vmx->pml_pg);
  4963. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  4964. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  4965. }
  4966. }
  4967. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  4968. {
  4969. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4970. struct msr_data apic_base_msr;
  4971. u64 cr0;
  4972. vmx->rmode.vm86_active = 0;
  4973. vmx->spec_ctrl = 0;
  4974. vcpu->arch.microcode_version = 0x100000000ULL;
  4975. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  4976. kvm_set_cr8(vcpu, 0);
  4977. if (!init_event) {
  4978. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  4979. MSR_IA32_APICBASE_ENABLE;
  4980. if (kvm_vcpu_is_reset_bsp(vcpu))
  4981. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  4982. apic_base_msr.host_initiated = true;
  4983. kvm_set_apic_base(vcpu, &apic_base_msr);
  4984. }
  4985. vmx_segment_cache_clear(vmx);
  4986. seg_setup(VCPU_SREG_CS);
  4987. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  4988. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  4989. seg_setup(VCPU_SREG_DS);
  4990. seg_setup(VCPU_SREG_ES);
  4991. seg_setup(VCPU_SREG_FS);
  4992. seg_setup(VCPU_SREG_GS);
  4993. seg_setup(VCPU_SREG_SS);
  4994. vmcs_write16(GUEST_TR_SELECTOR, 0);
  4995. vmcs_writel(GUEST_TR_BASE, 0);
  4996. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  4997. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4998. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  4999. vmcs_writel(GUEST_LDTR_BASE, 0);
  5000. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5001. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5002. if (!init_event) {
  5003. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5004. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5005. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5006. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5007. }
  5008. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5009. kvm_rip_write(vcpu, 0xfff0);
  5010. vmcs_writel(GUEST_GDTR_BASE, 0);
  5011. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5012. vmcs_writel(GUEST_IDTR_BASE, 0);
  5013. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5014. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5015. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5016. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5017. if (kvm_mpx_supported())
  5018. vmcs_write64(GUEST_BNDCFGS, 0);
  5019. setup_msrs(vmx);
  5020. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5021. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5022. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5023. if (cpu_need_tpr_shadow(vcpu))
  5024. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5025. __pa(vcpu->arch.apic->regs));
  5026. vmcs_write32(TPR_THRESHOLD, 0);
  5027. }
  5028. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5029. if (vmx->vpid != 0)
  5030. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5031. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5032. vmx->vcpu.arch.cr0 = cr0;
  5033. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5034. vmx_set_cr4(vcpu, 0);
  5035. vmx_set_efer(vcpu, 0);
  5036. update_exception_bitmap(vcpu);
  5037. vpid_sync_context(vmx->vpid);
  5038. }
  5039. /*
  5040. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5041. * For most existing hypervisors, this will always return true.
  5042. */
  5043. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5044. {
  5045. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5046. PIN_BASED_EXT_INTR_MASK;
  5047. }
  5048. /*
  5049. * In nested virtualization, check if L1 has set
  5050. * VM_EXIT_ACK_INTR_ON_EXIT
  5051. */
  5052. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5053. {
  5054. return get_vmcs12(vcpu)->vm_exit_controls &
  5055. VM_EXIT_ACK_INTR_ON_EXIT;
  5056. }
  5057. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5058. {
  5059. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5060. PIN_BASED_NMI_EXITING;
  5061. }
  5062. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5063. {
  5064. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5065. CPU_BASED_VIRTUAL_INTR_PENDING);
  5066. }
  5067. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5068. {
  5069. if (!enable_vnmi ||
  5070. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5071. enable_irq_window(vcpu);
  5072. return;
  5073. }
  5074. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5075. CPU_BASED_VIRTUAL_NMI_PENDING);
  5076. }
  5077. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5078. {
  5079. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5080. uint32_t intr;
  5081. int irq = vcpu->arch.interrupt.nr;
  5082. trace_kvm_inj_virq(irq);
  5083. ++vcpu->stat.irq_injections;
  5084. if (vmx->rmode.vm86_active) {
  5085. int inc_eip = 0;
  5086. if (vcpu->arch.interrupt.soft)
  5087. inc_eip = vcpu->arch.event_exit_inst_len;
  5088. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5089. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5090. return;
  5091. }
  5092. intr = irq | INTR_INFO_VALID_MASK;
  5093. if (vcpu->arch.interrupt.soft) {
  5094. intr |= INTR_TYPE_SOFT_INTR;
  5095. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5096. vmx->vcpu.arch.event_exit_inst_len);
  5097. } else
  5098. intr |= INTR_TYPE_EXT_INTR;
  5099. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5100. }
  5101. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5102. {
  5103. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5104. if (!enable_vnmi) {
  5105. /*
  5106. * Tracking the NMI-blocked state in software is built upon
  5107. * finding the next open IRQ window. This, in turn, depends on
  5108. * well-behaving guests: They have to keep IRQs disabled at
  5109. * least as long as the NMI handler runs. Otherwise we may
  5110. * cause NMI nesting, maybe breaking the guest. But as this is
  5111. * highly unlikely, we can live with the residual risk.
  5112. */
  5113. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5114. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5115. }
  5116. ++vcpu->stat.nmi_injections;
  5117. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5118. if (vmx->rmode.vm86_active) {
  5119. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5120. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5121. return;
  5122. }
  5123. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5124. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5125. }
  5126. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5127. {
  5128. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5129. bool masked;
  5130. if (!enable_vnmi)
  5131. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5132. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5133. return false;
  5134. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5135. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5136. return masked;
  5137. }
  5138. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5139. {
  5140. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5141. if (!enable_vnmi) {
  5142. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5143. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5144. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5145. }
  5146. } else {
  5147. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5148. if (masked)
  5149. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5150. GUEST_INTR_STATE_NMI);
  5151. else
  5152. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5153. GUEST_INTR_STATE_NMI);
  5154. }
  5155. }
  5156. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5157. {
  5158. if (to_vmx(vcpu)->nested.nested_run_pending)
  5159. return 0;
  5160. if (!enable_vnmi &&
  5161. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5162. return 0;
  5163. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5164. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5165. | GUEST_INTR_STATE_NMI));
  5166. }
  5167. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5168. {
  5169. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  5170. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5171. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5172. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5173. }
  5174. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5175. {
  5176. int ret;
  5177. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5178. PAGE_SIZE * 3);
  5179. if (ret)
  5180. return ret;
  5181. kvm->arch.tss_addr = addr;
  5182. return init_rmode_tss(kvm);
  5183. }
  5184. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5185. {
  5186. switch (vec) {
  5187. case BP_VECTOR:
  5188. /*
  5189. * Update instruction length as we may reinject the exception
  5190. * from user space while in guest debugging mode.
  5191. */
  5192. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5193. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5194. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5195. return false;
  5196. /* fall through */
  5197. case DB_VECTOR:
  5198. if (vcpu->guest_debug &
  5199. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  5200. return false;
  5201. /* fall through */
  5202. case DE_VECTOR:
  5203. case OF_VECTOR:
  5204. case BR_VECTOR:
  5205. case UD_VECTOR:
  5206. case DF_VECTOR:
  5207. case SS_VECTOR:
  5208. case GP_VECTOR:
  5209. case MF_VECTOR:
  5210. return true;
  5211. break;
  5212. }
  5213. return false;
  5214. }
  5215. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  5216. int vec, u32 err_code)
  5217. {
  5218. /*
  5219. * Instruction with address size override prefix opcode 0x67
  5220. * Cause the #SS fault with 0 error code in VM86 mode.
  5221. */
  5222. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  5223. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  5224. if (vcpu->arch.halt_request) {
  5225. vcpu->arch.halt_request = 0;
  5226. return kvm_vcpu_halt(vcpu);
  5227. }
  5228. return 1;
  5229. }
  5230. return 0;
  5231. }
  5232. /*
  5233. * Forward all other exceptions that are valid in real mode.
  5234. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  5235. * the required debugging infrastructure rework.
  5236. */
  5237. kvm_queue_exception(vcpu, vec);
  5238. return 1;
  5239. }
  5240. /*
  5241. * Trigger machine check on the host. We assume all the MSRs are already set up
  5242. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  5243. * We pass a fake environment to the machine check handler because we want
  5244. * the guest to be always treated like user space, no matter what context
  5245. * it used internally.
  5246. */
  5247. static void kvm_machine_check(void)
  5248. {
  5249. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  5250. struct pt_regs regs = {
  5251. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  5252. .flags = X86_EFLAGS_IF,
  5253. };
  5254. do_machine_check(&regs, 0);
  5255. #endif
  5256. }
  5257. static int handle_machine_check(struct kvm_vcpu *vcpu)
  5258. {
  5259. /* already handled by vcpu_run */
  5260. return 1;
  5261. }
  5262. static int handle_exception(struct kvm_vcpu *vcpu)
  5263. {
  5264. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5265. struct kvm_run *kvm_run = vcpu->run;
  5266. u32 intr_info, ex_no, error_code;
  5267. unsigned long cr2, rip, dr6;
  5268. u32 vect_info;
  5269. enum emulation_result er;
  5270. vect_info = vmx->idt_vectoring_info;
  5271. intr_info = vmx->exit_intr_info;
  5272. if (is_machine_check(intr_info))
  5273. return handle_machine_check(vcpu);
  5274. if (is_nmi(intr_info))
  5275. return 1; /* already handled by vmx_vcpu_run() */
  5276. if (is_invalid_opcode(intr_info)) {
  5277. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  5278. if (er == EMULATE_USER_EXIT)
  5279. return 0;
  5280. if (er != EMULATE_DONE)
  5281. kvm_queue_exception(vcpu, UD_VECTOR);
  5282. return 1;
  5283. }
  5284. error_code = 0;
  5285. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  5286. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  5287. /*
  5288. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  5289. * MMIO, it is better to report an internal error.
  5290. * See the comments in vmx_handle_exit.
  5291. */
  5292. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  5293. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  5294. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5295. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  5296. vcpu->run->internal.ndata = 3;
  5297. vcpu->run->internal.data[0] = vect_info;
  5298. vcpu->run->internal.data[1] = intr_info;
  5299. vcpu->run->internal.data[2] = error_code;
  5300. return 0;
  5301. }
  5302. if (is_page_fault(intr_info)) {
  5303. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  5304. /* EPT won't cause page fault directly */
  5305. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  5306. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  5307. }
  5308. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  5309. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  5310. return handle_rmode_exception(vcpu, ex_no, error_code);
  5311. switch (ex_no) {
  5312. case AC_VECTOR:
  5313. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  5314. return 1;
  5315. case DB_VECTOR:
  5316. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  5317. if (!(vcpu->guest_debug &
  5318. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  5319. vcpu->arch.dr6 &= ~15;
  5320. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  5321. if (!(dr6 & ~DR6_RESERVED)) /* icebp */
  5322. skip_emulated_instruction(vcpu);
  5323. kvm_queue_exception(vcpu, DB_VECTOR);
  5324. return 1;
  5325. }
  5326. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  5327. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  5328. /* fall through */
  5329. case BP_VECTOR:
  5330. /*
  5331. * Update instruction length as we may reinject #BP from
  5332. * user space while in guest debugging mode. Reading it for
  5333. * #DB as well causes no harm, it is not used in that case.
  5334. */
  5335. vmx->vcpu.arch.event_exit_inst_len =
  5336. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5337. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  5338. rip = kvm_rip_read(vcpu);
  5339. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  5340. kvm_run->debug.arch.exception = ex_no;
  5341. break;
  5342. default:
  5343. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  5344. kvm_run->ex.exception = ex_no;
  5345. kvm_run->ex.error_code = error_code;
  5346. break;
  5347. }
  5348. return 0;
  5349. }
  5350. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  5351. {
  5352. ++vcpu->stat.irq_exits;
  5353. return 1;
  5354. }
  5355. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  5356. {
  5357. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  5358. vcpu->mmio_needed = 0;
  5359. return 0;
  5360. }
  5361. static int handle_io(struct kvm_vcpu *vcpu)
  5362. {
  5363. unsigned long exit_qualification;
  5364. int size, in, string, ret;
  5365. unsigned port;
  5366. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5367. string = (exit_qualification & 16) != 0;
  5368. in = (exit_qualification & 8) != 0;
  5369. ++vcpu->stat.io_exits;
  5370. if (string || in)
  5371. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5372. port = exit_qualification >> 16;
  5373. size = (exit_qualification & 7) + 1;
  5374. ret = kvm_skip_emulated_instruction(vcpu);
  5375. /*
  5376. * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
  5377. * KVM_EXIT_DEBUG here.
  5378. */
  5379. return kvm_fast_pio_out(vcpu, size, port) && ret;
  5380. }
  5381. static void
  5382. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  5383. {
  5384. /*
  5385. * Patch in the VMCALL instruction:
  5386. */
  5387. hypercall[0] = 0x0f;
  5388. hypercall[1] = 0x01;
  5389. hypercall[2] = 0xc1;
  5390. }
  5391. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  5392. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  5393. {
  5394. if (is_guest_mode(vcpu)) {
  5395. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5396. unsigned long orig_val = val;
  5397. /*
  5398. * We get here when L2 changed cr0 in a way that did not change
  5399. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  5400. * but did change L0 shadowed bits. So we first calculate the
  5401. * effective cr0 value that L1 would like to write into the
  5402. * hardware. It consists of the L2-owned bits from the new
  5403. * value combined with the L1-owned bits from L1's guest_cr0.
  5404. */
  5405. val = (val & ~vmcs12->cr0_guest_host_mask) |
  5406. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  5407. if (!nested_guest_cr0_valid(vcpu, val))
  5408. return 1;
  5409. if (kvm_set_cr0(vcpu, val))
  5410. return 1;
  5411. vmcs_writel(CR0_READ_SHADOW, orig_val);
  5412. return 0;
  5413. } else {
  5414. if (to_vmx(vcpu)->nested.vmxon &&
  5415. !nested_host_cr0_valid(vcpu, val))
  5416. return 1;
  5417. return kvm_set_cr0(vcpu, val);
  5418. }
  5419. }
  5420. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  5421. {
  5422. if (is_guest_mode(vcpu)) {
  5423. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5424. unsigned long orig_val = val;
  5425. /* analogously to handle_set_cr0 */
  5426. val = (val & ~vmcs12->cr4_guest_host_mask) |
  5427. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  5428. if (kvm_set_cr4(vcpu, val))
  5429. return 1;
  5430. vmcs_writel(CR4_READ_SHADOW, orig_val);
  5431. return 0;
  5432. } else
  5433. return kvm_set_cr4(vcpu, val);
  5434. }
  5435. static int handle_desc(struct kvm_vcpu *vcpu)
  5436. {
  5437. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  5438. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5439. }
  5440. static int handle_cr(struct kvm_vcpu *vcpu)
  5441. {
  5442. unsigned long exit_qualification, val;
  5443. int cr;
  5444. int reg;
  5445. int err;
  5446. int ret;
  5447. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5448. cr = exit_qualification & 15;
  5449. reg = (exit_qualification >> 8) & 15;
  5450. switch ((exit_qualification >> 4) & 3) {
  5451. case 0: /* mov to cr */
  5452. val = kvm_register_readl(vcpu, reg);
  5453. trace_kvm_cr_write(cr, val);
  5454. switch (cr) {
  5455. case 0:
  5456. err = handle_set_cr0(vcpu, val);
  5457. return kvm_complete_insn_gp(vcpu, err);
  5458. case 3:
  5459. err = kvm_set_cr3(vcpu, val);
  5460. return kvm_complete_insn_gp(vcpu, err);
  5461. case 4:
  5462. err = handle_set_cr4(vcpu, val);
  5463. return kvm_complete_insn_gp(vcpu, err);
  5464. case 8: {
  5465. u8 cr8_prev = kvm_get_cr8(vcpu);
  5466. u8 cr8 = (u8)val;
  5467. err = kvm_set_cr8(vcpu, cr8);
  5468. ret = kvm_complete_insn_gp(vcpu, err);
  5469. if (lapic_in_kernel(vcpu))
  5470. return ret;
  5471. if (cr8_prev <= cr8)
  5472. return ret;
  5473. /*
  5474. * TODO: we might be squashing a
  5475. * KVM_GUESTDBG_SINGLESTEP-triggered
  5476. * KVM_EXIT_DEBUG here.
  5477. */
  5478. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  5479. return 0;
  5480. }
  5481. }
  5482. break;
  5483. case 2: /* clts */
  5484. WARN_ONCE(1, "Guest should always own CR0.TS");
  5485. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  5486. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  5487. return kvm_skip_emulated_instruction(vcpu);
  5488. case 1: /*mov from cr*/
  5489. switch (cr) {
  5490. case 3:
  5491. val = kvm_read_cr3(vcpu);
  5492. kvm_register_write(vcpu, reg, val);
  5493. trace_kvm_cr_read(cr, val);
  5494. return kvm_skip_emulated_instruction(vcpu);
  5495. case 8:
  5496. val = kvm_get_cr8(vcpu);
  5497. kvm_register_write(vcpu, reg, val);
  5498. trace_kvm_cr_read(cr, val);
  5499. return kvm_skip_emulated_instruction(vcpu);
  5500. }
  5501. break;
  5502. case 3: /* lmsw */
  5503. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  5504. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  5505. kvm_lmsw(vcpu, val);
  5506. return kvm_skip_emulated_instruction(vcpu);
  5507. default:
  5508. break;
  5509. }
  5510. vcpu->run->exit_reason = 0;
  5511. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  5512. (int)(exit_qualification >> 4) & 3, cr);
  5513. return 0;
  5514. }
  5515. static int handle_dr(struct kvm_vcpu *vcpu)
  5516. {
  5517. unsigned long exit_qualification;
  5518. int dr, dr7, reg;
  5519. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5520. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  5521. /* First, if DR does not exist, trigger UD */
  5522. if (!kvm_require_dr(vcpu, dr))
  5523. return 1;
  5524. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  5525. if (!kvm_require_cpl(vcpu, 0))
  5526. return 1;
  5527. dr7 = vmcs_readl(GUEST_DR7);
  5528. if (dr7 & DR7_GD) {
  5529. /*
  5530. * As the vm-exit takes precedence over the debug trap, we
  5531. * need to emulate the latter, either for the host or the
  5532. * guest debugging itself.
  5533. */
  5534. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  5535. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  5536. vcpu->run->debug.arch.dr7 = dr7;
  5537. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  5538. vcpu->run->debug.arch.exception = DB_VECTOR;
  5539. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  5540. return 0;
  5541. } else {
  5542. vcpu->arch.dr6 &= ~15;
  5543. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  5544. kvm_queue_exception(vcpu, DB_VECTOR);
  5545. return 1;
  5546. }
  5547. }
  5548. if (vcpu->guest_debug == 0) {
  5549. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  5550. CPU_BASED_MOV_DR_EXITING);
  5551. /*
  5552. * No more DR vmexits; force a reload of the debug registers
  5553. * and reenter on this instruction. The next vmexit will
  5554. * retrieve the full state of the debug registers.
  5555. */
  5556. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  5557. return 1;
  5558. }
  5559. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  5560. if (exit_qualification & TYPE_MOV_FROM_DR) {
  5561. unsigned long val;
  5562. if (kvm_get_dr(vcpu, dr, &val))
  5563. return 1;
  5564. kvm_register_write(vcpu, reg, val);
  5565. } else
  5566. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  5567. return 1;
  5568. return kvm_skip_emulated_instruction(vcpu);
  5569. }
  5570. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  5571. {
  5572. return vcpu->arch.dr6;
  5573. }
  5574. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  5575. {
  5576. }
  5577. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  5578. {
  5579. get_debugreg(vcpu->arch.db[0], 0);
  5580. get_debugreg(vcpu->arch.db[1], 1);
  5581. get_debugreg(vcpu->arch.db[2], 2);
  5582. get_debugreg(vcpu->arch.db[3], 3);
  5583. get_debugreg(vcpu->arch.dr6, 6);
  5584. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  5585. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  5586. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  5587. }
  5588. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  5589. {
  5590. vmcs_writel(GUEST_DR7, val);
  5591. }
  5592. static int handle_cpuid(struct kvm_vcpu *vcpu)
  5593. {
  5594. return kvm_emulate_cpuid(vcpu);
  5595. }
  5596. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  5597. {
  5598. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  5599. struct msr_data msr_info;
  5600. msr_info.index = ecx;
  5601. msr_info.host_initiated = false;
  5602. if (vmx_get_msr(vcpu, &msr_info)) {
  5603. trace_kvm_msr_read_ex(ecx);
  5604. kvm_inject_gp(vcpu, 0);
  5605. return 1;
  5606. }
  5607. trace_kvm_msr_read(ecx, msr_info.data);
  5608. /* FIXME: handling of bits 32:63 of rax, rdx */
  5609. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  5610. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  5611. return kvm_skip_emulated_instruction(vcpu);
  5612. }
  5613. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  5614. {
  5615. struct msr_data msr;
  5616. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  5617. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  5618. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  5619. msr.data = data;
  5620. msr.index = ecx;
  5621. msr.host_initiated = false;
  5622. if (kvm_set_msr(vcpu, &msr) != 0) {
  5623. trace_kvm_msr_write_ex(ecx, data);
  5624. kvm_inject_gp(vcpu, 0);
  5625. return 1;
  5626. }
  5627. trace_kvm_msr_write(ecx, data);
  5628. return kvm_skip_emulated_instruction(vcpu);
  5629. }
  5630. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  5631. {
  5632. kvm_apic_update_ppr(vcpu);
  5633. return 1;
  5634. }
  5635. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  5636. {
  5637. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  5638. CPU_BASED_VIRTUAL_INTR_PENDING);
  5639. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5640. ++vcpu->stat.irq_window_exits;
  5641. return 1;
  5642. }
  5643. static int handle_halt(struct kvm_vcpu *vcpu)
  5644. {
  5645. return kvm_emulate_halt(vcpu);
  5646. }
  5647. static int handle_vmcall(struct kvm_vcpu *vcpu)
  5648. {
  5649. return kvm_emulate_hypercall(vcpu);
  5650. }
  5651. static int handle_invd(struct kvm_vcpu *vcpu)
  5652. {
  5653. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5654. }
  5655. static int handle_invlpg(struct kvm_vcpu *vcpu)
  5656. {
  5657. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5658. kvm_mmu_invlpg(vcpu, exit_qualification);
  5659. return kvm_skip_emulated_instruction(vcpu);
  5660. }
  5661. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  5662. {
  5663. int err;
  5664. err = kvm_rdpmc(vcpu);
  5665. return kvm_complete_insn_gp(vcpu, err);
  5666. }
  5667. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  5668. {
  5669. return kvm_emulate_wbinvd(vcpu);
  5670. }
  5671. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  5672. {
  5673. u64 new_bv = kvm_read_edx_eax(vcpu);
  5674. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  5675. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  5676. return kvm_skip_emulated_instruction(vcpu);
  5677. return 1;
  5678. }
  5679. static int handle_xsaves(struct kvm_vcpu *vcpu)
  5680. {
  5681. kvm_skip_emulated_instruction(vcpu);
  5682. WARN(1, "this should never happen\n");
  5683. return 1;
  5684. }
  5685. static int handle_xrstors(struct kvm_vcpu *vcpu)
  5686. {
  5687. kvm_skip_emulated_instruction(vcpu);
  5688. WARN(1, "this should never happen\n");
  5689. return 1;
  5690. }
  5691. static int handle_apic_access(struct kvm_vcpu *vcpu)
  5692. {
  5693. if (likely(fasteoi)) {
  5694. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5695. int access_type, offset;
  5696. access_type = exit_qualification & APIC_ACCESS_TYPE;
  5697. offset = exit_qualification & APIC_ACCESS_OFFSET;
  5698. /*
  5699. * Sane guest uses MOV to write EOI, with written value
  5700. * not cared. So make a short-circuit here by avoiding
  5701. * heavy instruction emulation.
  5702. */
  5703. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  5704. (offset == APIC_EOI)) {
  5705. kvm_lapic_set_eoi(vcpu);
  5706. return kvm_skip_emulated_instruction(vcpu);
  5707. }
  5708. }
  5709. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  5710. }
  5711. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  5712. {
  5713. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5714. int vector = exit_qualification & 0xff;
  5715. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  5716. kvm_apic_set_eoi_accelerated(vcpu, vector);
  5717. return 1;
  5718. }
  5719. static int handle_apic_write(struct kvm_vcpu *vcpu)
  5720. {
  5721. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5722. u32 offset = exit_qualification & 0xfff;
  5723. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  5724. kvm_apic_write_nodecode(vcpu, offset);
  5725. return 1;
  5726. }
  5727. static int handle_task_switch(struct kvm_vcpu *vcpu)
  5728. {
  5729. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5730. unsigned long exit_qualification;
  5731. bool has_error_code = false;
  5732. u32 error_code = 0;
  5733. u16 tss_selector;
  5734. int reason, type, idt_v, idt_index;
  5735. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  5736. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  5737. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  5738. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5739. reason = (u32)exit_qualification >> 30;
  5740. if (reason == TASK_SWITCH_GATE && idt_v) {
  5741. switch (type) {
  5742. case INTR_TYPE_NMI_INTR:
  5743. vcpu->arch.nmi_injected = false;
  5744. vmx_set_nmi_mask(vcpu, true);
  5745. break;
  5746. case INTR_TYPE_EXT_INTR:
  5747. case INTR_TYPE_SOFT_INTR:
  5748. kvm_clear_interrupt_queue(vcpu);
  5749. break;
  5750. case INTR_TYPE_HARD_EXCEPTION:
  5751. if (vmx->idt_vectoring_info &
  5752. VECTORING_INFO_DELIVER_CODE_MASK) {
  5753. has_error_code = true;
  5754. error_code =
  5755. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  5756. }
  5757. /* fall through */
  5758. case INTR_TYPE_SOFT_EXCEPTION:
  5759. kvm_clear_exception_queue(vcpu);
  5760. break;
  5761. default:
  5762. break;
  5763. }
  5764. }
  5765. tss_selector = exit_qualification;
  5766. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  5767. type != INTR_TYPE_EXT_INTR &&
  5768. type != INTR_TYPE_NMI_INTR))
  5769. skip_emulated_instruction(vcpu);
  5770. if (kvm_task_switch(vcpu, tss_selector,
  5771. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  5772. has_error_code, error_code) == EMULATE_FAIL) {
  5773. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5774. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  5775. vcpu->run->internal.ndata = 0;
  5776. return 0;
  5777. }
  5778. /*
  5779. * TODO: What about debug traps on tss switch?
  5780. * Are we supposed to inject them and update dr6?
  5781. */
  5782. return 1;
  5783. }
  5784. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  5785. {
  5786. unsigned long exit_qualification;
  5787. gpa_t gpa;
  5788. u64 error_code;
  5789. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5790. /*
  5791. * EPT violation happened while executing iret from NMI,
  5792. * "blocked by NMI" bit has to be set before next VM entry.
  5793. * There are errata that may cause this bit to not be set:
  5794. * AAK134, BY25.
  5795. */
  5796. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  5797. enable_vnmi &&
  5798. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  5799. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  5800. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  5801. trace_kvm_page_fault(gpa, exit_qualification);
  5802. /* Is it a read fault? */
  5803. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  5804. ? PFERR_USER_MASK : 0;
  5805. /* Is it a write fault? */
  5806. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  5807. ? PFERR_WRITE_MASK : 0;
  5808. /* Is it a fetch fault? */
  5809. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  5810. ? PFERR_FETCH_MASK : 0;
  5811. /* ept page table entry is present? */
  5812. error_code |= (exit_qualification &
  5813. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  5814. EPT_VIOLATION_EXECUTABLE))
  5815. ? PFERR_PRESENT_MASK : 0;
  5816. error_code |= (exit_qualification & 0x100) != 0 ?
  5817. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  5818. vcpu->arch.exit_qualification = exit_qualification;
  5819. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  5820. }
  5821. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  5822. {
  5823. int ret;
  5824. gpa_t gpa;
  5825. /*
  5826. * A nested guest cannot optimize MMIO vmexits, because we have an
  5827. * nGPA here instead of the required GPA.
  5828. */
  5829. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  5830. if (!is_guest_mode(vcpu) &&
  5831. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  5832. trace_kvm_fast_mmio(gpa);
  5833. /*
  5834. * Doing kvm_skip_emulated_instruction() depends on undefined
  5835. * behavior: Intel's manual doesn't mandate
  5836. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  5837. * occurs and while on real hardware it was observed to be set,
  5838. * other hypervisors (namely Hyper-V) don't set it, we end up
  5839. * advancing IP with some random value. Disable fast mmio when
  5840. * running nested and keep it for real hardware in hope that
  5841. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  5842. */
  5843. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  5844. return kvm_skip_emulated_instruction(vcpu);
  5845. else
  5846. return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
  5847. NULL, 0) == EMULATE_DONE;
  5848. }
  5849. ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  5850. if (ret >= 0)
  5851. return ret;
  5852. /* It is the real ept misconfig */
  5853. WARN_ON(1);
  5854. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  5855. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  5856. return 0;
  5857. }
  5858. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  5859. {
  5860. WARN_ON_ONCE(!enable_vnmi);
  5861. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  5862. CPU_BASED_VIRTUAL_NMI_PENDING);
  5863. ++vcpu->stat.nmi_window_exits;
  5864. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5865. return 1;
  5866. }
  5867. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  5868. {
  5869. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5870. enum emulation_result err = EMULATE_DONE;
  5871. int ret = 1;
  5872. u32 cpu_exec_ctrl;
  5873. bool intr_window_requested;
  5874. unsigned count = 130;
  5875. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  5876. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  5877. while (vmx->emulation_required && count-- != 0) {
  5878. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  5879. return handle_interrupt_window(&vmx->vcpu);
  5880. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  5881. return 1;
  5882. err = emulate_instruction(vcpu, 0);
  5883. if (err == EMULATE_USER_EXIT) {
  5884. ++vcpu->stat.mmio_exits;
  5885. ret = 0;
  5886. goto out;
  5887. }
  5888. if (err != EMULATE_DONE) {
  5889. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  5890. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  5891. vcpu->run->internal.ndata = 0;
  5892. return 0;
  5893. }
  5894. if (vcpu->arch.halt_request) {
  5895. vcpu->arch.halt_request = 0;
  5896. ret = kvm_vcpu_halt(vcpu);
  5897. goto out;
  5898. }
  5899. if (signal_pending(current))
  5900. goto out;
  5901. if (need_resched())
  5902. schedule();
  5903. }
  5904. out:
  5905. return ret;
  5906. }
  5907. static int __grow_ple_window(int val)
  5908. {
  5909. if (ple_window_grow < 1)
  5910. return ple_window;
  5911. val = min(val, ple_window_actual_max);
  5912. if (ple_window_grow < ple_window)
  5913. val *= ple_window_grow;
  5914. else
  5915. val += ple_window_grow;
  5916. return val;
  5917. }
  5918. static int __shrink_ple_window(int val, int modifier, int minimum)
  5919. {
  5920. if (modifier < 1)
  5921. return ple_window;
  5922. if (modifier < ple_window)
  5923. val /= modifier;
  5924. else
  5925. val -= modifier;
  5926. return max(val, minimum);
  5927. }
  5928. static void grow_ple_window(struct kvm_vcpu *vcpu)
  5929. {
  5930. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5931. int old = vmx->ple_window;
  5932. vmx->ple_window = __grow_ple_window(old);
  5933. if (vmx->ple_window != old)
  5934. vmx->ple_window_dirty = true;
  5935. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  5936. }
  5937. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  5938. {
  5939. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5940. int old = vmx->ple_window;
  5941. vmx->ple_window = __shrink_ple_window(old,
  5942. ple_window_shrink, ple_window);
  5943. if (vmx->ple_window != old)
  5944. vmx->ple_window_dirty = true;
  5945. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  5946. }
  5947. /*
  5948. * ple_window_actual_max is computed to be one grow_ple_window() below
  5949. * ple_window_max. (See __grow_ple_window for the reason.)
  5950. * This prevents overflows, because ple_window_max is int.
  5951. * ple_window_max effectively rounded down to a multiple of ple_window_grow in
  5952. * this process.
  5953. * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
  5954. */
  5955. static void update_ple_window_actual_max(void)
  5956. {
  5957. ple_window_actual_max =
  5958. __shrink_ple_window(max(ple_window_max, ple_window),
  5959. ple_window_grow, INT_MIN);
  5960. }
  5961. /*
  5962. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  5963. */
  5964. static void wakeup_handler(void)
  5965. {
  5966. struct kvm_vcpu *vcpu;
  5967. int cpu = smp_processor_id();
  5968. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  5969. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  5970. blocked_vcpu_list) {
  5971. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  5972. if (pi_test_on(pi_desc) == 1)
  5973. kvm_vcpu_kick(vcpu);
  5974. }
  5975. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  5976. }
  5977. void vmx_enable_tdp(void)
  5978. {
  5979. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  5980. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  5981. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  5982. 0ull, VMX_EPT_EXECUTABLE_MASK,
  5983. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  5984. VMX_EPT_RWX_MASK, 0ull);
  5985. ept_set_mmio_spte_mask();
  5986. kvm_enable_tdp();
  5987. }
  5988. static __init int hardware_setup(void)
  5989. {
  5990. int r = -ENOMEM, i;
  5991. rdmsrl_safe(MSR_EFER, &host_efer);
  5992. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  5993. kvm_define_shared_msr(i, vmx_msr_index[i]);
  5994. for (i = 0; i < VMX_BITMAP_NR; i++) {
  5995. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  5996. if (!vmx_bitmap[i])
  5997. goto out;
  5998. }
  5999. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6000. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6001. if (setup_vmcs_config(&vmcs_config) < 0) {
  6002. r = -EIO;
  6003. goto out;
  6004. }
  6005. if (boot_cpu_has(X86_FEATURE_NX))
  6006. kvm_enable_efer_bits(EFER_NX);
  6007. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6008. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6009. enable_vpid = 0;
  6010. if (!cpu_has_vmx_ept() ||
  6011. !cpu_has_vmx_ept_4levels() ||
  6012. !cpu_has_vmx_ept_mt_wb() ||
  6013. !cpu_has_vmx_invept_global())
  6014. enable_ept = 0;
  6015. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6016. enable_ept_ad_bits = 0;
  6017. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6018. enable_unrestricted_guest = 0;
  6019. if (!cpu_has_vmx_flexpriority())
  6020. flexpriority_enabled = 0;
  6021. if (!cpu_has_virtual_nmis())
  6022. enable_vnmi = 0;
  6023. /*
  6024. * set_apic_access_page_addr() is used to reload apic access
  6025. * page upon invalidation. No need to do anything if not
  6026. * using the APIC_ACCESS_ADDR VMCS field.
  6027. */
  6028. if (!flexpriority_enabled)
  6029. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6030. if (!cpu_has_vmx_tpr_shadow())
  6031. kvm_x86_ops->update_cr8_intercept = NULL;
  6032. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6033. kvm_disable_largepages();
  6034. if (!cpu_has_vmx_ple()) {
  6035. ple_gap = 0;
  6036. ple_window = 0;
  6037. ple_window_grow = 0;
  6038. ple_window_max = 0;
  6039. ple_window_shrink = 0;
  6040. }
  6041. if (!cpu_has_vmx_apicv()) {
  6042. enable_apicv = 0;
  6043. kvm_x86_ops->sync_pir_to_irr = NULL;
  6044. }
  6045. if (cpu_has_vmx_tsc_scaling()) {
  6046. kvm_has_tsc_control = true;
  6047. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6048. kvm_tsc_scaling_ratio_frac_bits = 48;
  6049. }
  6050. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6051. if (enable_ept)
  6052. vmx_enable_tdp();
  6053. else
  6054. kvm_disable_tdp();
  6055. update_ple_window_actual_max();
  6056. /*
  6057. * Only enable PML when hardware supports PML feature, and both EPT
  6058. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6059. */
  6060. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6061. enable_pml = 0;
  6062. if (!enable_pml) {
  6063. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6064. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6065. kvm_x86_ops->flush_log_dirty = NULL;
  6066. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6067. }
  6068. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6069. u64 vmx_msr;
  6070. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6071. cpu_preemption_timer_multi =
  6072. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6073. } else {
  6074. kvm_x86_ops->set_hv_timer = NULL;
  6075. kvm_x86_ops->cancel_hv_timer = NULL;
  6076. }
  6077. if (!cpu_has_vmx_shadow_vmcs())
  6078. enable_shadow_vmcs = 0;
  6079. if (enable_shadow_vmcs)
  6080. init_vmcs_shadow_fields();
  6081. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6082. kvm_mce_cap_supported |= MCG_LMCE_P;
  6083. return alloc_kvm_area();
  6084. out:
  6085. for (i = 0; i < VMX_BITMAP_NR; i++)
  6086. free_page((unsigned long)vmx_bitmap[i]);
  6087. return r;
  6088. }
  6089. static __exit void hardware_unsetup(void)
  6090. {
  6091. int i;
  6092. for (i = 0; i < VMX_BITMAP_NR; i++)
  6093. free_page((unsigned long)vmx_bitmap[i]);
  6094. free_kvm_area();
  6095. }
  6096. /*
  6097. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6098. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6099. */
  6100. static int handle_pause(struct kvm_vcpu *vcpu)
  6101. {
  6102. if (ple_gap)
  6103. grow_ple_window(vcpu);
  6104. /*
  6105. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6106. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6107. * never set PAUSE_EXITING and just set PLE if supported,
  6108. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6109. */
  6110. kvm_vcpu_on_spin(vcpu, true);
  6111. return kvm_skip_emulated_instruction(vcpu);
  6112. }
  6113. static int handle_nop(struct kvm_vcpu *vcpu)
  6114. {
  6115. return kvm_skip_emulated_instruction(vcpu);
  6116. }
  6117. static int handle_mwait(struct kvm_vcpu *vcpu)
  6118. {
  6119. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6120. return handle_nop(vcpu);
  6121. }
  6122. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6123. {
  6124. kvm_queue_exception(vcpu, UD_VECTOR);
  6125. return 1;
  6126. }
  6127. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6128. {
  6129. return 1;
  6130. }
  6131. static int handle_monitor(struct kvm_vcpu *vcpu)
  6132. {
  6133. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6134. return handle_nop(vcpu);
  6135. }
  6136. /*
  6137. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6138. * set the success or error code of an emulated VMX instruction, as specified
  6139. * by Vol 2B, VMX Instruction Reference, "Conventions".
  6140. */
  6141. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6142. {
  6143. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6144. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6145. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6146. }
  6147. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6148. {
  6149. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6150. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6151. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6152. | X86_EFLAGS_CF);
  6153. }
  6154. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6155. u32 vm_instruction_error)
  6156. {
  6157. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  6158. /*
  6159. * failValid writes the error number to the current VMCS, which
  6160. * can't be done there isn't a current VMCS.
  6161. */
  6162. nested_vmx_failInvalid(vcpu);
  6163. return;
  6164. }
  6165. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6166. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6167. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6168. | X86_EFLAGS_ZF);
  6169. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6170. /*
  6171. * We don't need to force a shadow sync because
  6172. * VM_INSTRUCTION_ERROR is not shadowed
  6173. */
  6174. }
  6175. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6176. {
  6177. /* TODO: not to reset guest simply here. */
  6178. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6179. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6180. }
  6181. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6182. {
  6183. struct vcpu_vmx *vmx =
  6184. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6185. vmx->nested.preemption_timer_expired = true;
  6186. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6187. kvm_vcpu_kick(&vmx->vcpu);
  6188. return HRTIMER_NORESTART;
  6189. }
  6190. /*
  6191. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6192. * exit caused by such an instruction (run by a guest hypervisor).
  6193. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6194. * #UD or #GP.
  6195. */
  6196. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6197. unsigned long exit_qualification,
  6198. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6199. {
  6200. gva_t off;
  6201. bool exn;
  6202. struct kvm_segment s;
  6203. /*
  6204. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6205. * Execution", on an exit, vmx_instruction_info holds most of the
  6206. * addressing components of the operand. Only the displacement part
  6207. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6208. * For how an actual address is calculated from all these components,
  6209. * refer to Vol. 1, "Operand Addressing".
  6210. */
  6211. int scaling = vmx_instruction_info & 3;
  6212. int addr_size = (vmx_instruction_info >> 7) & 7;
  6213. bool is_reg = vmx_instruction_info & (1u << 10);
  6214. int seg_reg = (vmx_instruction_info >> 15) & 7;
  6215. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  6216. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  6217. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  6218. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  6219. if (is_reg) {
  6220. kvm_queue_exception(vcpu, UD_VECTOR);
  6221. return 1;
  6222. }
  6223. /* Addr = segment_base + offset */
  6224. /* offset = base + [index * scale] + displacement */
  6225. off = exit_qualification; /* holds the displacement */
  6226. if (base_is_valid)
  6227. off += kvm_register_read(vcpu, base_reg);
  6228. if (index_is_valid)
  6229. off += kvm_register_read(vcpu, index_reg)<<scaling;
  6230. vmx_get_segment(vcpu, &s, seg_reg);
  6231. *ret = s.base + off;
  6232. if (addr_size == 1) /* 32 bit */
  6233. *ret &= 0xffffffff;
  6234. /* Checks for #GP/#SS exceptions. */
  6235. exn = false;
  6236. if (is_long_mode(vcpu)) {
  6237. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  6238. * non-canonical form. This is the only check on the memory
  6239. * destination for long mode!
  6240. */
  6241. exn = is_noncanonical_address(*ret, vcpu);
  6242. } else if (is_protmode(vcpu)) {
  6243. /* Protected mode: apply checks for segment validity in the
  6244. * following order:
  6245. * - segment type check (#GP(0) may be thrown)
  6246. * - usability check (#GP(0)/#SS(0))
  6247. * - limit check (#GP(0)/#SS(0))
  6248. */
  6249. if (wr)
  6250. /* #GP(0) if the destination operand is located in a
  6251. * read-only data segment or any code segment.
  6252. */
  6253. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  6254. else
  6255. /* #GP(0) if the source operand is located in an
  6256. * execute-only code segment
  6257. */
  6258. exn = ((s.type & 0xa) == 8);
  6259. if (exn) {
  6260. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  6261. return 1;
  6262. }
  6263. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  6264. */
  6265. exn = (s.unusable != 0);
  6266. /* Protected mode: #GP(0)/#SS(0) if the memory
  6267. * operand is outside the segment limit.
  6268. */
  6269. exn = exn || (off + sizeof(u64) > s.limit);
  6270. }
  6271. if (exn) {
  6272. kvm_queue_exception_e(vcpu,
  6273. seg_reg == VCPU_SREG_SS ?
  6274. SS_VECTOR : GP_VECTOR,
  6275. 0);
  6276. return 1;
  6277. }
  6278. return 0;
  6279. }
  6280. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  6281. {
  6282. gva_t gva;
  6283. struct x86_exception e;
  6284. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6285. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  6286. return 1;
  6287. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
  6288. sizeof(*vmpointer), &e)) {
  6289. kvm_inject_page_fault(vcpu, &e);
  6290. return 1;
  6291. }
  6292. return 0;
  6293. }
  6294. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  6295. {
  6296. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6297. struct vmcs *shadow_vmcs;
  6298. int r;
  6299. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  6300. if (r < 0)
  6301. goto out_vmcs02;
  6302. vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  6303. if (!vmx->nested.cached_vmcs12)
  6304. goto out_cached_vmcs12;
  6305. if (enable_shadow_vmcs) {
  6306. shadow_vmcs = alloc_vmcs();
  6307. if (!shadow_vmcs)
  6308. goto out_shadow_vmcs;
  6309. /* mark vmcs as shadow */
  6310. shadow_vmcs->revision_id |= (1u << 31);
  6311. /* init shadow vmcs */
  6312. vmcs_clear(shadow_vmcs);
  6313. vmx->vmcs01.shadow_vmcs = shadow_vmcs;
  6314. }
  6315. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  6316. HRTIMER_MODE_REL_PINNED);
  6317. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  6318. vmx->nested.vmxon = true;
  6319. return 0;
  6320. out_shadow_vmcs:
  6321. kfree(vmx->nested.cached_vmcs12);
  6322. out_cached_vmcs12:
  6323. free_loaded_vmcs(&vmx->nested.vmcs02);
  6324. out_vmcs02:
  6325. return -ENOMEM;
  6326. }
  6327. /*
  6328. * Emulate the VMXON instruction.
  6329. * Currently, we just remember that VMX is active, and do not save or even
  6330. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  6331. * do not currently need to store anything in that guest-allocated memory
  6332. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  6333. * argument is different from the VMXON pointer (which the spec says they do).
  6334. */
  6335. static int handle_vmon(struct kvm_vcpu *vcpu)
  6336. {
  6337. int ret;
  6338. gpa_t vmptr;
  6339. struct page *page;
  6340. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6341. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  6342. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  6343. /*
  6344. * The Intel VMX Instruction Reference lists a bunch of bits that are
  6345. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  6346. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  6347. * Otherwise, we should fail with #UD. But most faulting conditions
  6348. * have already been checked by hardware, prior to the VM-exit for
  6349. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  6350. * that bit set to 1 in non-root mode.
  6351. */
  6352. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  6353. kvm_queue_exception(vcpu, UD_VECTOR);
  6354. return 1;
  6355. }
  6356. if (vmx->nested.vmxon) {
  6357. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  6358. return kvm_skip_emulated_instruction(vcpu);
  6359. }
  6360. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  6361. != VMXON_NEEDED_FEATURES) {
  6362. kvm_inject_gp(vcpu, 0);
  6363. return 1;
  6364. }
  6365. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6366. return 1;
  6367. /*
  6368. * SDM 3: 24.11.5
  6369. * The first 4 bytes of VMXON region contain the supported
  6370. * VMCS revision identifier
  6371. *
  6372. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  6373. * which replaces physical address width with 32
  6374. */
  6375. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6376. nested_vmx_failInvalid(vcpu);
  6377. return kvm_skip_emulated_instruction(vcpu);
  6378. }
  6379. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  6380. if (is_error_page(page)) {
  6381. nested_vmx_failInvalid(vcpu);
  6382. return kvm_skip_emulated_instruction(vcpu);
  6383. }
  6384. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  6385. kunmap(page);
  6386. kvm_release_page_clean(page);
  6387. nested_vmx_failInvalid(vcpu);
  6388. return kvm_skip_emulated_instruction(vcpu);
  6389. }
  6390. kunmap(page);
  6391. kvm_release_page_clean(page);
  6392. vmx->nested.vmxon_ptr = vmptr;
  6393. ret = enter_vmx_operation(vcpu);
  6394. if (ret)
  6395. return ret;
  6396. nested_vmx_succeed(vcpu);
  6397. return kvm_skip_emulated_instruction(vcpu);
  6398. }
  6399. /*
  6400. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  6401. * for running VMX instructions (except VMXON, whose prerequisites are
  6402. * slightly different). It also specifies what exception to inject otherwise.
  6403. * Note that many of these exceptions have priority over VM exits, so they
  6404. * don't have to be checked again here.
  6405. */
  6406. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  6407. {
  6408. if (!to_vmx(vcpu)->nested.vmxon) {
  6409. kvm_queue_exception(vcpu, UD_VECTOR);
  6410. return 0;
  6411. }
  6412. return 1;
  6413. }
  6414. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  6415. {
  6416. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  6417. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  6418. }
  6419. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  6420. {
  6421. if (vmx->nested.current_vmptr == -1ull)
  6422. return;
  6423. if (enable_shadow_vmcs) {
  6424. /* copy to memory all shadowed fields in case
  6425. they were modified */
  6426. copy_shadow_to_vmcs12(vmx);
  6427. vmx->nested.sync_shadow_vmcs = false;
  6428. vmx_disable_shadow_vmcs(vmx);
  6429. }
  6430. vmx->nested.posted_intr_nv = -1;
  6431. /* Flush VMCS12 to guest memory */
  6432. kvm_vcpu_write_guest_page(&vmx->vcpu,
  6433. vmx->nested.current_vmptr >> PAGE_SHIFT,
  6434. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  6435. vmx->nested.current_vmptr = -1ull;
  6436. }
  6437. /*
  6438. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  6439. * just stops using VMX.
  6440. */
  6441. static void free_nested(struct vcpu_vmx *vmx)
  6442. {
  6443. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  6444. return;
  6445. vmx->nested.vmxon = false;
  6446. vmx->nested.smm.vmxon = false;
  6447. free_vpid(vmx->nested.vpid02);
  6448. vmx->nested.posted_intr_nv = -1;
  6449. vmx->nested.current_vmptr = -1ull;
  6450. if (enable_shadow_vmcs) {
  6451. vmx_disable_shadow_vmcs(vmx);
  6452. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  6453. free_vmcs(vmx->vmcs01.shadow_vmcs);
  6454. vmx->vmcs01.shadow_vmcs = NULL;
  6455. }
  6456. kfree(vmx->nested.cached_vmcs12);
  6457. /* Unpin physical memory we referred to in the vmcs02 */
  6458. if (vmx->nested.apic_access_page) {
  6459. kvm_release_page_dirty(vmx->nested.apic_access_page);
  6460. vmx->nested.apic_access_page = NULL;
  6461. }
  6462. if (vmx->nested.virtual_apic_page) {
  6463. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  6464. vmx->nested.virtual_apic_page = NULL;
  6465. }
  6466. if (vmx->nested.pi_desc_page) {
  6467. kunmap(vmx->nested.pi_desc_page);
  6468. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  6469. vmx->nested.pi_desc_page = NULL;
  6470. vmx->nested.pi_desc = NULL;
  6471. }
  6472. free_loaded_vmcs(&vmx->nested.vmcs02);
  6473. }
  6474. /* Emulate the VMXOFF instruction */
  6475. static int handle_vmoff(struct kvm_vcpu *vcpu)
  6476. {
  6477. if (!nested_vmx_check_permission(vcpu))
  6478. return 1;
  6479. free_nested(to_vmx(vcpu));
  6480. nested_vmx_succeed(vcpu);
  6481. return kvm_skip_emulated_instruction(vcpu);
  6482. }
  6483. /* Emulate the VMCLEAR instruction */
  6484. static int handle_vmclear(struct kvm_vcpu *vcpu)
  6485. {
  6486. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6487. u32 zero = 0;
  6488. gpa_t vmptr;
  6489. if (!nested_vmx_check_permission(vcpu))
  6490. return 1;
  6491. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6492. return 1;
  6493. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6494. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  6495. return kvm_skip_emulated_instruction(vcpu);
  6496. }
  6497. if (vmptr == vmx->nested.vmxon_ptr) {
  6498. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
  6499. return kvm_skip_emulated_instruction(vcpu);
  6500. }
  6501. if (vmptr == vmx->nested.current_vmptr)
  6502. nested_release_vmcs12(vmx);
  6503. kvm_vcpu_write_guest(vcpu,
  6504. vmptr + offsetof(struct vmcs12, launch_state),
  6505. &zero, sizeof(zero));
  6506. nested_vmx_succeed(vcpu);
  6507. return kvm_skip_emulated_instruction(vcpu);
  6508. }
  6509. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  6510. /* Emulate the VMLAUNCH instruction */
  6511. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  6512. {
  6513. return nested_vmx_run(vcpu, true);
  6514. }
  6515. /* Emulate the VMRESUME instruction */
  6516. static int handle_vmresume(struct kvm_vcpu *vcpu)
  6517. {
  6518. return nested_vmx_run(vcpu, false);
  6519. }
  6520. /*
  6521. * Read a vmcs12 field. Since these can have varying lengths and we return
  6522. * one type, we chose the biggest type (u64) and zero-extend the return value
  6523. * to that size. Note that the caller, handle_vmread, might need to use only
  6524. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  6525. * 64-bit fields are to be returned).
  6526. */
  6527. static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
  6528. unsigned long field, u64 *ret)
  6529. {
  6530. short offset = vmcs_field_to_offset(field);
  6531. char *p;
  6532. if (offset < 0)
  6533. return offset;
  6534. p = ((char *)(get_vmcs12(vcpu))) + offset;
  6535. switch (vmcs_field_width(field)) {
  6536. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  6537. *ret = *((natural_width *)p);
  6538. return 0;
  6539. case VMCS_FIELD_WIDTH_U16:
  6540. *ret = *((u16 *)p);
  6541. return 0;
  6542. case VMCS_FIELD_WIDTH_U32:
  6543. *ret = *((u32 *)p);
  6544. return 0;
  6545. case VMCS_FIELD_WIDTH_U64:
  6546. *ret = *((u64 *)p);
  6547. return 0;
  6548. default:
  6549. WARN_ON(1);
  6550. return -ENOENT;
  6551. }
  6552. }
  6553. static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
  6554. unsigned long field, u64 field_value){
  6555. short offset = vmcs_field_to_offset(field);
  6556. char *p = ((char *) get_vmcs12(vcpu)) + offset;
  6557. if (offset < 0)
  6558. return offset;
  6559. switch (vmcs_field_width(field)) {
  6560. case VMCS_FIELD_WIDTH_U16:
  6561. *(u16 *)p = field_value;
  6562. return 0;
  6563. case VMCS_FIELD_WIDTH_U32:
  6564. *(u32 *)p = field_value;
  6565. return 0;
  6566. case VMCS_FIELD_WIDTH_U64:
  6567. *(u64 *)p = field_value;
  6568. return 0;
  6569. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  6570. *(natural_width *)p = field_value;
  6571. return 0;
  6572. default:
  6573. WARN_ON(1);
  6574. return -ENOENT;
  6575. }
  6576. }
  6577. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  6578. {
  6579. int i;
  6580. unsigned long field;
  6581. u64 field_value;
  6582. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  6583. const u16 *fields = shadow_read_write_fields;
  6584. const int num_fields = max_shadow_read_write_fields;
  6585. preempt_disable();
  6586. vmcs_load(shadow_vmcs);
  6587. for (i = 0; i < num_fields; i++) {
  6588. field = fields[i];
  6589. field_value = __vmcs_readl(field);
  6590. vmcs12_write_any(&vmx->vcpu, field, field_value);
  6591. }
  6592. vmcs_clear(shadow_vmcs);
  6593. vmcs_load(vmx->loaded_vmcs->vmcs);
  6594. preempt_enable();
  6595. }
  6596. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  6597. {
  6598. const u16 *fields[] = {
  6599. shadow_read_write_fields,
  6600. shadow_read_only_fields
  6601. };
  6602. const int max_fields[] = {
  6603. max_shadow_read_write_fields,
  6604. max_shadow_read_only_fields
  6605. };
  6606. int i, q;
  6607. unsigned long field;
  6608. u64 field_value = 0;
  6609. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  6610. vmcs_load(shadow_vmcs);
  6611. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  6612. for (i = 0; i < max_fields[q]; i++) {
  6613. field = fields[q][i];
  6614. vmcs12_read_any(&vmx->vcpu, field, &field_value);
  6615. __vmcs_writel(field, field_value);
  6616. }
  6617. }
  6618. vmcs_clear(shadow_vmcs);
  6619. vmcs_load(vmx->loaded_vmcs->vmcs);
  6620. }
  6621. /*
  6622. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  6623. * used before) all generate the same failure when it is missing.
  6624. */
  6625. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  6626. {
  6627. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6628. if (vmx->nested.current_vmptr == -1ull) {
  6629. nested_vmx_failInvalid(vcpu);
  6630. return 0;
  6631. }
  6632. return 1;
  6633. }
  6634. static int handle_vmread(struct kvm_vcpu *vcpu)
  6635. {
  6636. unsigned long field;
  6637. u64 field_value;
  6638. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6639. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6640. gva_t gva = 0;
  6641. if (!nested_vmx_check_permission(vcpu))
  6642. return 1;
  6643. if (!nested_vmx_check_vmcs12(vcpu))
  6644. return kvm_skip_emulated_instruction(vcpu);
  6645. /* Decode instruction info and find the field to read */
  6646. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  6647. /* Read the field, zero-extended to a u64 field_value */
  6648. if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
  6649. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  6650. return kvm_skip_emulated_instruction(vcpu);
  6651. }
  6652. /*
  6653. * Now copy part of this value to register or memory, as requested.
  6654. * Note that the number of bits actually copied is 32 or 64 depending
  6655. * on the guest's mode (32 or 64 bit), not on the given field's length.
  6656. */
  6657. if (vmx_instruction_info & (1u << 10)) {
  6658. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  6659. field_value);
  6660. } else {
  6661. if (get_vmx_mem_address(vcpu, exit_qualification,
  6662. vmx_instruction_info, true, &gva))
  6663. return 1;
  6664. /* _system ok, as hardware has verified cpl=0 */
  6665. kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
  6666. &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
  6667. }
  6668. nested_vmx_succeed(vcpu);
  6669. return kvm_skip_emulated_instruction(vcpu);
  6670. }
  6671. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  6672. {
  6673. unsigned long field;
  6674. gva_t gva;
  6675. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6676. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6677. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6678. /* The value to write might be 32 or 64 bits, depending on L1's long
  6679. * mode, and eventually we need to write that into a field of several
  6680. * possible lengths. The code below first zero-extends the value to 64
  6681. * bit (field_value), and then copies only the appropriate number of
  6682. * bits into the vmcs12 field.
  6683. */
  6684. u64 field_value = 0;
  6685. struct x86_exception e;
  6686. if (!nested_vmx_check_permission(vcpu))
  6687. return 1;
  6688. if (!nested_vmx_check_vmcs12(vcpu))
  6689. return kvm_skip_emulated_instruction(vcpu);
  6690. if (vmx_instruction_info & (1u << 10))
  6691. field_value = kvm_register_readl(vcpu,
  6692. (((vmx_instruction_info) >> 3) & 0xf));
  6693. else {
  6694. if (get_vmx_mem_address(vcpu, exit_qualification,
  6695. vmx_instruction_info, false, &gva))
  6696. return 1;
  6697. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
  6698. &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  6699. kvm_inject_page_fault(vcpu, &e);
  6700. return 1;
  6701. }
  6702. }
  6703. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  6704. if (vmcs_field_readonly(field)) {
  6705. nested_vmx_failValid(vcpu,
  6706. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  6707. return kvm_skip_emulated_instruction(vcpu);
  6708. }
  6709. if (vmcs12_write_any(vcpu, field, field_value) < 0) {
  6710. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  6711. return kvm_skip_emulated_instruction(vcpu);
  6712. }
  6713. switch (field) {
  6714. #define SHADOW_FIELD_RW(x) case x:
  6715. #include "vmx_shadow_fields.h"
  6716. /*
  6717. * The fields that can be updated by L1 without a vmexit are
  6718. * always updated in the vmcs02, the others go down the slow
  6719. * path of prepare_vmcs02.
  6720. */
  6721. break;
  6722. default:
  6723. vmx->nested.dirty_vmcs12 = true;
  6724. break;
  6725. }
  6726. nested_vmx_succeed(vcpu);
  6727. return kvm_skip_emulated_instruction(vcpu);
  6728. }
  6729. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  6730. {
  6731. vmx->nested.current_vmptr = vmptr;
  6732. if (enable_shadow_vmcs) {
  6733. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  6734. SECONDARY_EXEC_SHADOW_VMCS);
  6735. vmcs_write64(VMCS_LINK_POINTER,
  6736. __pa(vmx->vmcs01.shadow_vmcs));
  6737. vmx->nested.sync_shadow_vmcs = true;
  6738. }
  6739. vmx->nested.dirty_vmcs12 = true;
  6740. }
  6741. /* Emulate the VMPTRLD instruction */
  6742. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  6743. {
  6744. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6745. gpa_t vmptr;
  6746. if (!nested_vmx_check_permission(vcpu))
  6747. return 1;
  6748. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  6749. return 1;
  6750. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  6751. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  6752. return kvm_skip_emulated_instruction(vcpu);
  6753. }
  6754. if (vmptr == vmx->nested.vmxon_ptr) {
  6755. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
  6756. return kvm_skip_emulated_instruction(vcpu);
  6757. }
  6758. if (vmx->nested.current_vmptr != vmptr) {
  6759. struct vmcs12 *new_vmcs12;
  6760. struct page *page;
  6761. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  6762. if (is_error_page(page)) {
  6763. nested_vmx_failInvalid(vcpu);
  6764. return kvm_skip_emulated_instruction(vcpu);
  6765. }
  6766. new_vmcs12 = kmap(page);
  6767. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  6768. kunmap(page);
  6769. kvm_release_page_clean(page);
  6770. nested_vmx_failValid(vcpu,
  6771. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  6772. return kvm_skip_emulated_instruction(vcpu);
  6773. }
  6774. nested_release_vmcs12(vmx);
  6775. /*
  6776. * Load VMCS12 from guest memory since it is not already
  6777. * cached.
  6778. */
  6779. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  6780. kunmap(page);
  6781. kvm_release_page_clean(page);
  6782. set_current_vmptr(vmx, vmptr);
  6783. }
  6784. nested_vmx_succeed(vcpu);
  6785. return kvm_skip_emulated_instruction(vcpu);
  6786. }
  6787. /* Emulate the VMPTRST instruction */
  6788. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  6789. {
  6790. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6791. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6792. gva_t vmcs_gva;
  6793. struct x86_exception e;
  6794. if (!nested_vmx_check_permission(vcpu))
  6795. return 1;
  6796. if (get_vmx_mem_address(vcpu, exit_qualification,
  6797. vmx_instruction_info, true, &vmcs_gva))
  6798. return 1;
  6799. /* ok to use *_system, as hardware has verified cpl=0 */
  6800. if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
  6801. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  6802. sizeof(u64), &e)) {
  6803. kvm_inject_page_fault(vcpu, &e);
  6804. return 1;
  6805. }
  6806. nested_vmx_succeed(vcpu);
  6807. return kvm_skip_emulated_instruction(vcpu);
  6808. }
  6809. /* Emulate the INVEPT instruction */
  6810. static int handle_invept(struct kvm_vcpu *vcpu)
  6811. {
  6812. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6813. u32 vmx_instruction_info, types;
  6814. unsigned long type;
  6815. gva_t gva;
  6816. struct x86_exception e;
  6817. struct {
  6818. u64 eptp, gpa;
  6819. } operand;
  6820. if (!(vmx->nested.nested_vmx_secondary_ctls_high &
  6821. SECONDARY_EXEC_ENABLE_EPT) ||
  6822. !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
  6823. kvm_queue_exception(vcpu, UD_VECTOR);
  6824. return 1;
  6825. }
  6826. if (!nested_vmx_check_permission(vcpu))
  6827. return 1;
  6828. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6829. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  6830. types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  6831. if (type >= 32 || !(types & (1 << type))) {
  6832. nested_vmx_failValid(vcpu,
  6833. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6834. return kvm_skip_emulated_instruction(vcpu);
  6835. }
  6836. /* According to the Intel VMX instruction reference, the memory
  6837. * operand is read even if it isn't needed (e.g., for type==global)
  6838. */
  6839. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6840. vmx_instruction_info, false, &gva))
  6841. return 1;
  6842. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
  6843. sizeof(operand), &e)) {
  6844. kvm_inject_page_fault(vcpu, &e);
  6845. return 1;
  6846. }
  6847. switch (type) {
  6848. case VMX_EPT_EXTENT_GLOBAL:
  6849. /*
  6850. * TODO: track mappings and invalidate
  6851. * single context requests appropriately
  6852. */
  6853. case VMX_EPT_EXTENT_CONTEXT:
  6854. kvm_mmu_sync_roots(vcpu);
  6855. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  6856. nested_vmx_succeed(vcpu);
  6857. break;
  6858. default:
  6859. BUG_ON(1);
  6860. break;
  6861. }
  6862. return kvm_skip_emulated_instruction(vcpu);
  6863. }
  6864. static int handle_invvpid(struct kvm_vcpu *vcpu)
  6865. {
  6866. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6867. u32 vmx_instruction_info;
  6868. unsigned long type, types;
  6869. gva_t gva;
  6870. struct x86_exception e;
  6871. struct {
  6872. u64 vpid;
  6873. u64 gla;
  6874. } operand;
  6875. if (!(vmx->nested.nested_vmx_secondary_ctls_high &
  6876. SECONDARY_EXEC_ENABLE_VPID) ||
  6877. !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
  6878. kvm_queue_exception(vcpu, UD_VECTOR);
  6879. return 1;
  6880. }
  6881. if (!nested_vmx_check_permission(vcpu))
  6882. return 1;
  6883. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  6884. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  6885. types = (vmx->nested.nested_vmx_vpid_caps &
  6886. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  6887. if (type >= 32 || !(types & (1 << type))) {
  6888. nested_vmx_failValid(vcpu,
  6889. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6890. return kvm_skip_emulated_instruction(vcpu);
  6891. }
  6892. /* according to the intel vmx instruction reference, the memory
  6893. * operand is read even if it isn't needed (e.g., for type==global)
  6894. */
  6895. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  6896. vmx_instruction_info, false, &gva))
  6897. return 1;
  6898. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
  6899. sizeof(operand), &e)) {
  6900. kvm_inject_page_fault(vcpu, &e);
  6901. return 1;
  6902. }
  6903. if (operand.vpid >> 16) {
  6904. nested_vmx_failValid(vcpu,
  6905. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6906. return kvm_skip_emulated_instruction(vcpu);
  6907. }
  6908. switch (type) {
  6909. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  6910. if (is_noncanonical_address(operand.gla, vcpu)) {
  6911. nested_vmx_failValid(vcpu,
  6912. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6913. return kvm_skip_emulated_instruction(vcpu);
  6914. }
  6915. /* fall through */
  6916. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  6917. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  6918. if (!operand.vpid) {
  6919. nested_vmx_failValid(vcpu,
  6920. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  6921. return kvm_skip_emulated_instruction(vcpu);
  6922. }
  6923. break;
  6924. case VMX_VPID_EXTENT_ALL_CONTEXT:
  6925. break;
  6926. default:
  6927. WARN_ON_ONCE(1);
  6928. return kvm_skip_emulated_instruction(vcpu);
  6929. }
  6930. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  6931. nested_vmx_succeed(vcpu);
  6932. return kvm_skip_emulated_instruction(vcpu);
  6933. }
  6934. static int handle_pml_full(struct kvm_vcpu *vcpu)
  6935. {
  6936. unsigned long exit_qualification;
  6937. trace_kvm_pml_full(vcpu->vcpu_id);
  6938. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6939. /*
  6940. * PML buffer FULL happened while executing iret from NMI,
  6941. * "blocked by NMI" bit has to be set before next VM entry.
  6942. */
  6943. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6944. enable_vnmi &&
  6945. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6946. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  6947. GUEST_INTR_STATE_NMI);
  6948. /*
  6949. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  6950. * here.., and there's no userspace involvement needed for PML.
  6951. */
  6952. return 1;
  6953. }
  6954. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  6955. {
  6956. kvm_lapic_expired_hv_timer(vcpu);
  6957. return 1;
  6958. }
  6959. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  6960. {
  6961. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6962. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  6963. /* Check for memory type validity */
  6964. switch (address & VMX_EPTP_MT_MASK) {
  6965. case VMX_EPTP_MT_UC:
  6966. if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
  6967. return false;
  6968. break;
  6969. case VMX_EPTP_MT_WB:
  6970. if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
  6971. return false;
  6972. break;
  6973. default:
  6974. return false;
  6975. }
  6976. /* only 4 levels page-walk length are valid */
  6977. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  6978. return false;
  6979. /* Reserved bits should not be set */
  6980. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  6981. return false;
  6982. /* AD, if set, should be supported */
  6983. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  6984. if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
  6985. return false;
  6986. }
  6987. return true;
  6988. }
  6989. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  6990. struct vmcs12 *vmcs12)
  6991. {
  6992. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  6993. u64 address;
  6994. bool accessed_dirty;
  6995. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  6996. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  6997. !nested_cpu_has_ept(vmcs12))
  6998. return 1;
  6999. if (index >= VMFUNC_EPTP_ENTRIES)
  7000. return 1;
  7001. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  7002. &address, index * 8, 8))
  7003. return 1;
  7004. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  7005. /*
  7006. * If the (L2) guest does a vmfunc to the currently
  7007. * active ept pointer, we don't have to do anything else
  7008. */
  7009. if (vmcs12->ept_pointer != address) {
  7010. if (!valid_ept_address(vcpu, address))
  7011. return 1;
  7012. kvm_mmu_unload(vcpu);
  7013. mmu->ept_ad = accessed_dirty;
  7014. mmu->base_role.ad_disabled = !accessed_dirty;
  7015. vmcs12->ept_pointer = address;
  7016. /*
  7017. * TODO: Check what's the correct approach in case
  7018. * mmu reload fails. Currently, we just let the next
  7019. * reload potentially fail
  7020. */
  7021. kvm_mmu_reload(vcpu);
  7022. }
  7023. return 0;
  7024. }
  7025. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  7026. {
  7027. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7028. struct vmcs12 *vmcs12;
  7029. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  7030. /*
  7031. * VMFUNC is only supported for nested guests, but we always enable the
  7032. * secondary control for simplicity; for non-nested mode, fake that we
  7033. * didn't by injecting #UD.
  7034. */
  7035. if (!is_guest_mode(vcpu)) {
  7036. kvm_queue_exception(vcpu, UD_VECTOR);
  7037. return 1;
  7038. }
  7039. vmcs12 = get_vmcs12(vcpu);
  7040. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  7041. goto fail;
  7042. switch (function) {
  7043. case 0:
  7044. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  7045. goto fail;
  7046. break;
  7047. default:
  7048. goto fail;
  7049. }
  7050. return kvm_skip_emulated_instruction(vcpu);
  7051. fail:
  7052. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  7053. vmcs_read32(VM_EXIT_INTR_INFO),
  7054. vmcs_readl(EXIT_QUALIFICATION));
  7055. return 1;
  7056. }
  7057. /*
  7058. * The exit handlers return 1 if the exit was handled fully and guest execution
  7059. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  7060. * to be done to userspace and return 0.
  7061. */
  7062. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  7063. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  7064. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  7065. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  7066. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  7067. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  7068. [EXIT_REASON_CR_ACCESS] = handle_cr,
  7069. [EXIT_REASON_DR_ACCESS] = handle_dr,
  7070. [EXIT_REASON_CPUID] = handle_cpuid,
  7071. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  7072. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  7073. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  7074. [EXIT_REASON_HLT] = handle_halt,
  7075. [EXIT_REASON_INVD] = handle_invd,
  7076. [EXIT_REASON_INVLPG] = handle_invlpg,
  7077. [EXIT_REASON_RDPMC] = handle_rdpmc,
  7078. [EXIT_REASON_VMCALL] = handle_vmcall,
  7079. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  7080. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  7081. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  7082. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  7083. [EXIT_REASON_VMREAD] = handle_vmread,
  7084. [EXIT_REASON_VMRESUME] = handle_vmresume,
  7085. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  7086. [EXIT_REASON_VMOFF] = handle_vmoff,
  7087. [EXIT_REASON_VMON] = handle_vmon,
  7088. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  7089. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  7090. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  7091. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  7092. [EXIT_REASON_WBINVD] = handle_wbinvd,
  7093. [EXIT_REASON_XSETBV] = handle_xsetbv,
  7094. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  7095. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  7096. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  7097. [EXIT_REASON_LDTR_TR] = handle_desc,
  7098. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  7099. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  7100. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  7101. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  7102. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  7103. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  7104. [EXIT_REASON_INVEPT] = handle_invept,
  7105. [EXIT_REASON_INVVPID] = handle_invvpid,
  7106. [EXIT_REASON_RDRAND] = handle_invalid_op,
  7107. [EXIT_REASON_RDSEED] = handle_invalid_op,
  7108. [EXIT_REASON_XSAVES] = handle_xsaves,
  7109. [EXIT_REASON_XRSTORS] = handle_xrstors,
  7110. [EXIT_REASON_PML_FULL] = handle_pml_full,
  7111. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  7112. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  7113. };
  7114. static const int kvm_vmx_max_exit_handlers =
  7115. ARRAY_SIZE(kvm_vmx_exit_handlers);
  7116. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  7117. struct vmcs12 *vmcs12)
  7118. {
  7119. unsigned long exit_qualification;
  7120. gpa_t bitmap, last_bitmap;
  7121. unsigned int port;
  7122. int size;
  7123. u8 b;
  7124. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  7125. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  7126. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7127. port = exit_qualification >> 16;
  7128. size = (exit_qualification & 7) + 1;
  7129. last_bitmap = (gpa_t)-1;
  7130. b = -1;
  7131. while (size > 0) {
  7132. if (port < 0x8000)
  7133. bitmap = vmcs12->io_bitmap_a;
  7134. else if (port < 0x10000)
  7135. bitmap = vmcs12->io_bitmap_b;
  7136. else
  7137. return true;
  7138. bitmap += (port & 0x7fff) / 8;
  7139. if (last_bitmap != bitmap)
  7140. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  7141. return true;
  7142. if (b & (1 << (port & 7)))
  7143. return true;
  7144. port++;
  7145. size--;
  7146. last_bitmap = bitmap;
  7147. }
  7148. return false;
  7149. }
  7150. /*
  7151. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  7152. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  7153. * disinterest in the current event (read or write a specific MSR) by using an
  7154. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  7155. */
  7156. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  7157. struct vmcs12 *vmcs12, u32 exit_reason)
  7158. {
  7159. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  7160. gpa_t bitmap;
  7161. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  7162. return true;
  7163. /*
  7164. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  7165. * for the four combinations of read/write and low/high MSR numbers.
  7166. * First we need to figure out which of the four to use:
  7167. */
  7168. bitmap = vmcs12->msr_bitmap;
  7169. if (exit_reason == EXIT_REASON_MSR_WRITE)
  7170. bitmap += 2048;
  7171. if (msr_index >= 0xc0000000) {
  7172. msr_index -= 0xc0000000;
  7173. bitmap += 1024;
  7174. }
  7175. /* Then read the msr_index'th bit from this bitmap: */
  7176. if (msr_index < 1024*8) {
  7177. unsigned char b;
  7178. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  7179. return true;
  7180. return 1 & (b >> (msr_index & 7));
  7181. } else
  7182. return true; /* let L1 handle the wrong parameter */
  7183. }
  7184. /*
  7185. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  7186. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  7187. * intercept (via guest_host_mask etc.) the current event.
  7188. */
  7189. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  7190. struct vmcs12 *vmcs12)
  7191. {
  7192. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7193. int cr = exit_qualification & 15;
  7194. int reg;
  7195. unsigned long val;
  7196. switch ((exit_qualification >> 4) & 3) {
  7197. case 0: /* mov to cr */
  7198. reg = (exit_qualification >> 8) & 15;
  7199. val = kvm_register_readl(vcpu, reg);
  7200. switch (cr) {
  7201. case 0:
  7202. if (vmcs12->cr0_guest_host_mask &
  7203. (val ^ vmcs12->cr0_read_shadow))
  7204. return true;
  7205. break;
  7206. case 3:
  7207. if ((vmcs12->cr3_target_count >= 1 &&
  7208. vmcs12->cr3_target_value0 == val) ||
  7209. (vmcs12->cr3_target_count >= 2 &&
  7210. vmcs12->cr3_target_value1 == val) ||
  7211. (vmcs12->cr3_target_count >= 3 &&
  7212. vmcs12->cr3_target_value2 == val) ||
  7213. (vmcs12->cr3_target_count >= 4 &&
  7214. vmcs12->cr3_target_value3 == val))
  7215. return false;
  7216. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  7217. return true;
  7218. break;
  7219. case 4:
  7220. if (vmcs12->cr4_guest_host_mask &
  7221. (vmcs12->cr4_read_shadow ^ val))
  7222. return true;
  7223. break;
  7224. case 8:
  7225. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  7226. return true;
  7227. break;
  7228. }
  7229. break;
  7230. case 2: /* clts */
  7231. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  7232. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  7233. return true;
  7234. break;
  7235. case 1: /* mov from cr */
  7236. switch (cr) {
  7237. case 3:
  7238. if (vmcs12->cpu_based_vm_exec_control &
  7239. CPU_BASED_CR3_STORE_EXITING)
  7240. return true;
  7241. break;
  7242. case 8:
  7243. if (vmcs12->cpu_based_vm_exec_control &
  7244. CPU_BASED_CR8_STORE_EXITING)
  7245. return true;
  7246. break;
  7247. }
  7248. break;
  7249. case 3: /* lmsw */
  7250. /*
  7251. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  7252. * cr0. Other attempted changes are ignored, with no exit.
  7253. */
  7254. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  7255. if (vmcs12->cr0_guest_host_mask & 0xe &
  7256. (val ^ vmcs12->cr0_read_shadow))
  7257. return true;
  7258. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  7259. !(vmcs12->cr0_read_shadow & 0x1) &&
  7260. (val & 0x1))
  7261. return true;
  7262. break;
  7263. }
  7264. return false;
  7265. }
  7266. /*
  7267. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  7268. * should handle it ourselves in L0 (and then continue L2). Only call this
  7269. * when in is_guest_mode (L2).
  7270. */
  7271. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  7272. {
  7273. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7274. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7275. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7276. if (vmx->nested.nested_run_pending)
  7277. return false;
  7278. if (unlikely(vmx->fail)) {
  7279. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  7280. vmcs_read32(VM_INSTRUCTION_ERROR));
  7281. return true;
  7282. }
  7283. /*
  7284. * The host physical addresses of some pages of guest memory
  7285. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  7286. * Page). The CPU may write to these pages via their host
  7287. * physical address while L2 is running, bypassing any
  7288. * address-translation-based dirty tracking (e.g. EPT write
  7289. * protection).
  7290. *
  7291. * Mark them dirty on every exit from L2 to prevent them from
  7292. * getting out of sync with dirty tracking.
  7293. */
  7294. nested_mark_vmcs12_pages_dirty(vcpu);
  7295. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  7296. vmcs_readl(EXIT_QUALIFICATION),
  7297. vmx->idt_vectoring_info,
  7298. intr_info,
  7299. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  7300. KVM_ISA_VMX);
  7301. switch (exit_reason) {
  7302. case EXIT_REASON_EXCEPTION_NMI:
  7303. if (is_nmi(intr_info))
  7304. return false;
  7305. else if (is_page_fault(intr_info))
  7306. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  7307. else if (is_no_device(intr_info) &&
  7308. !(vmcs12->guest_cr0 & X86_CR0_TS))
  7309. return false;
  7310. else if (is_debug(intr_info) &&
  7311. vcpu->guest_debug &
  7312. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  7313. return false;
  7314. else if (is_breakpoint(intr_info) &&
  7315. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  7316. return false;
  7317. return vmcs12->exception_bitmap &
  7318. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  7319. case EXIT_REASON_EXTERNAL_INTERRUPT:
  7320. return false;
  7321. case EXIT_REASON_TRIPLE_FAULT:
  7322. return true;
  7323. case EXIT_REASON_PENDING_INTERRUPT:
  7324. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  7325. case EXIT_REASON_NMI_WINDOW:
  7326. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  7327. case EXIT_REASON_TASK_SWITCH:
  7328. return true;
  7329. case EXIT_REASON_CPUID:
  7330. return true;
  7331. case EXIT_REASON_HLT:
  7332. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  7333. case EXIT_REASON_INVD:
  7334. return true;
  7335. case EXIT_REASON_INVLPG:
  7336. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7337. case EXIT_REASON_RDPMC:
  7338. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  7339. case EXIT_REASON_RDRAND:
  7340. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  7341. case EXIT_REASON_RDSEED:
  7342. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  7343. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  7344. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  7345. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  7346. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  7347. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  7348. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  7349. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  7350. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  7351. /*
  7352. * VMX instructions trap unconditionally. This allows L1 to
  7353. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  7354. */
  7355. return true;
  7356. case EXIT_REASON_CR_ACCESS:
  7357. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  7358. case EXIT_REASON_DR_ACCESS:
  7359. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  7360. case EXIT_REASON_IO_INSTRUCTION:
  7361. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  7362. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  7363. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  7364. case EXIT_REASON_MSR_READ:
  7365. case EXIT_REASON_MSR_WRITE:
  7366. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  7367. case EXIT_REASON_INVALID_STATE:
  7368. return true;
  7369. case EXIT_REASON_MWAIT_INSTRUCTION:
  7370. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  7371. case EXIT_REASON_MONITOR_TRAP_FLAG:
  7372. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  7373. case EXIT_REASON_MONITOR_INSTRUCTION:
  7374. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  7375. case EXIT_REASON_PAUSE_INSTRUCTION:
  7376. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  7377. nested_cpu_has2(vmcs12,
  7378. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  7379. case EXIT_REASON_MCE_DURING_VMENTRY:
  7380. return false;
  7381. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  7382. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  7383. case EXIT_REASON_APIC_ACCESS:
  7384. return nested_cpu_has2(vmcs12,
  7385. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  7386. case EXIT_REASON_APIC_WRITE:
  7387. case EXIT_REASON_EOI_INDUCED:
  7388. /* apic_write and eoi_induced should exit unconditionally. */
  7389. return true;
  7390. case EXIT_REASON_EPT_VIOLATION:
  7391. /*
  7392. * L0 always deals with the EPT violation. If nested EPT is
  7393. * used, and the nested mmu code discovers that the address is
  7394. * missing in the guest EPT table (EPT12), the EPT violation
  7395. * will be injected with nested_ept_inject_page_fault()
  7396. */
  7397. return false;
  7398. case EXIT_REASON_EPT_MISCONFIG:
  7399. /*
  7400. * L2 never uses directly L1's EPT, but rather L0's own EPT
  7401. * table (shadow on EPT) or a merged EPT table that L0 built
  7402. * (EPT on EPT). So any problems with the structure of the
  7403. * table is L0's fault.
  7404. */
  7405. return false;
  7406. case EXIT_REASON_INVPCID:
  7407. return
  7408. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  7409. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  7410. case EXIT_REASON_WBINVD:
  7411. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  7412. case EXIT_REASON_XSETBV:
  7413. return true;
  7414. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  7415. /*
  7416. * This should never happen, since it is not possible to
  7417. * set XSS to a non-zero value---neither in L1 nor in L2.
  7418. * If if it were, XSS would have to be checked against
  7419. * the XSS exit bitmap in vmcs12.
  7420. */
  7421. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  7422. case EXIT_REASON_PREEMPTION_TIMER:
  7423. return false;
  7424. case EXIT_REASON_PML_FULL:
  7425. /* We emulate PML support to L1. */
  7426. return false;
  7427. case EXIT_REASON_VMFUNC:
  7428. /* VM functions are emulated through L2->L0 vmexits. */
  7429. return false;
  7430. default:
  7431. return true;
  7432. }
  7433. }
  7434. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  7435. {
  7436. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7437. /*
  7438. * At this point, the exit interruption info in exit_intr_info
  7439. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  7440. * we need to query the in-kernel LAPIC.
  7441. */
  7442. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  7443. if ((exit_intr_info &
  7444. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  7445. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  7446. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7447. vmcs12->vm_exit_intr_error_code =
  7448. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  7449. }
  7450. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  7451. vmcs_readl(EXIT_QUALIFICATION));
  7452. return 1;
  7453. }
  7454. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  7455. {
  7456. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  7457. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  7458. }
  7459. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  7460. {
  7461. if (vmx->pml_pg) {
  7462. __free_page(vmx->pml_pg);
  7463. vmx->pml_pg = NULL;
  7464. }
  7465. }
  7466. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  7467. {
  7468. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7469. u64 *pml_buf;
  7470. u16 pml_idx;
  7471. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  7472. /* Do nothing if PML buffer is empty */
  7473. if (pml_idx == (PML_ENTITY_NUM - 1))
  7474. return;
  7475. /* PML index always points to next available PML buffer entity */
  7476. if (pml_idx >= PML_ENTITY_NUM)
  7477. pml_idx = 0;
  7478. else
  7479. pml_idx++;
  7480. pml_buf = page_address(vmx->pml_pg);
  7481. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  7482. u64 gpa;
  7483. gpa = pml_buf[pml_idx];
  7484. WARN_ON(gpa & (PAGE_SIZE - 1));
  7485. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  7486. }
  7487. /* reset PML index */
  7488. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  7489. }
  7490. /*
  7491. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  7492. * Called before reporting dirty_bitmap to userspace.
  7493. */
  7494. static void kvm_flush_pml_buffers(struct kvm *kvm)
  7495. {
  7496. int i;
  7497. struct kvm_vcpu *vcpu;
  7498. /*
  7499. * We only need to kick vcpu out of guest mode here, as PML buffer
  7500. * is flushed at beginning of all VMEXITs, and it's obvious that only
  7501. * vcpus running in guest are possible to have unflushed GPAs in PML
  7502. * buffer.
  7503. */
  7504. kvm_for_each_vcpu(i, vcpu, kvm)
  7505. kvm_vcpu_kick(vcpu);
  7506. }
  7507. static void vmx_dump_sel(char *name, uint32_t sel)
  7508. {
  7509. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  7510. name, vmcs_read16(sel),
  7511. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  7512. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  7513. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  7514. }
  7515. static void vmx_dump_dtsel(char *name, uint32_t limit)
  7516. {
  7517. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  7518. name, vmcs_read32(limit),
  7519. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  7520. }
  7521. static void dump_vmcs(void)
  7522. {
  7523. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  7524. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  7525. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  7526. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  7527. u32 secondary_exec_control = 0;
  7528. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  7529. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  7530. int i, n;
  7531. if (cpu_has_secondary_exec_ctrls())
  7532. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7533. pr_err("*** Guest State ***\n");
  7534. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  7535. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  7536. vmcs_readl(CR0_GUEST_HOST_MASK));
  7537. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  7538. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  7539. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  7540. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  7541. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  7542. {
  7543. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  7544. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  7545. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  7546. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  7547. }
  7548. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  7549. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  7550. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  7551. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  7552. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  7553. vmcs_readl(GUEST_SYSENTER_ESP),
  7554. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  7555. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  7556. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  7557. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  7558. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  7559. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  7560. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  7561. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  7562. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  7563. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  7564. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  7565. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  7566. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  7567. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  7568. efer, vmcs_read64(GUEST_IA32_PAT));
  7569. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  7570. vmcs_read64(GUEST_IA32_DEBUGCTL),
  7571. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  7572. if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  7573. pr_err("PerfGlobCtl = 0x%016llx\n",
  7574. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  7575. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  7576. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  7577. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  7578. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  7579. vmcs_read32(GUEST_ACTIVITY_STATE));
  7580. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  7581. pr_err("InterruptStatus = %04x\n",
  7582. vmcs_read16(GUEST_INTR_STATUS));
  7583. pr_err("*** Host State ***\n");
  7584. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  7585. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  7586. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  7587. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  7588. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  7589. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  7590. vmcs_read16(HOST_TR_SELECTOR));
  7591. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  7592. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  7593. vmcs_readl(HOST_TR_BASE));
  7594. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  7595. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  7596. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  7597. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  7598. vmcs_readl(HOST_CR4));
  7599. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  7600. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  7601. vmcs_read32(HOST_IA32_SYSENTER_CS),
  7602. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  7603. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  7604. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  7605. vmcs_read64(HOST_IA32_EFER),
  7606. vmcs_read64(HOST_IA32_PAT));
  7607. if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  7608. pr_err("PerfGlobCtl = 0x%016llx\n",
  7609. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  7610. pr_err("*** Control State ***\n");
  7611. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  7612. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  7613. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  7614. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  7615. vmcs_read32(EXCEPTION_BITMAP),
  7616. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  7617. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  7618. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  7619. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  7620. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  7621. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  7622. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  7623. vmcs_read32(VM_EXIT_INTR_INFO),
  7624. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  7625. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  7626. pr_err(" reason=%08x qualification=%016lx\n",
  7627. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  7628. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  7629. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  7630. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  7631. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  7632. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  7633. pr_err("TSC Multiplier = 0x%016llx\n",
  7634. vmcs_read64(TSC_MULTIPLIER));
  7635. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  7636. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  7637. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  7638. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  7639. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  7640. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  7641. n = vmcs_read32(CR3_TARGET_COUNT);
  7642. for (i = 0; i + 1 < n; i += 4)
  7643. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  7644. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  7645. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  7646. if (i < n)
  7647. pr_err("CR3 target%u=%016lx\n",
  7648. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  7649. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  7650. pr_err("PLE Gap=%08x Window=%08x\n",
  7651. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  7652. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  7653. pr_err("Virtual processor ID = 0x%04x\n",
  7654. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  7655. }
  7656. /*
  7657. * The guest has exited. See if we can fix it or if we need userspace
  7658. * assistance.
  7659. */
  7660. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  7661. {
  7662. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7663. u32 exit_reason = vmx->exit_reason;
  7664. u32 vectoring_info = vmx->idt_vectoring_info;
  7665. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  7666. /*
  7667. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  7668. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  7669. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  7670. * mode as if vcpus is in root mode, the PML buffer must has been
  7671. * flushed already.
  7672. */
  7673. if (enable_pml)
  7674. vmx_flush_pml_buffer(vcpu);
  7675. /* If guest state is invalid, start emulating */
  7676. if (vmx->emulation_required)
  7677. return handle_invalid_guest_state(vcpu);
  7678. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  7679. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  7680. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  7681. dump_vmcs();
  7682. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  7683. vcpu->run->fail_entry.hardware_entry_failure_reason
  7684. = exit_reason;
  7685. return 0;
  7686. }
  7687. if (unlikely(vmx->fail)) {
  7688. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  7689. vcpu->run->fail_entry.hardware_entry_failure_reason
  7690. = vmcs_read32(VM_INSTRUCTION_ERROR);
  7691. return 0;
  7692. }
  7693. /*
  7694. * Note:
  7695. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  7696. * delivery event since it indicates guest is accessing MMIO.
  7697. * The vm-exit can be triggered again after return to guest that
  7698. * will cause infinite loop.
  7699. */
  7700. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  7701. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  7702. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  7703. exit_reason != EXIT_REASON_PML_FULL &&
  7704. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  7705. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  7706. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  7707. vcpu->run->internal.ndata = 3;
  7708. vcpu->run->internal.data[0] = vectoring_info;
  7709. vcpu->run->internal.data[1] = exit_reason;
  7710. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  7711. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  7712. vcpu->run->internal.ndata++;
  7713. vcpu->run->internal.data[3] =
  7714. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  7715. }
  7716. return 0;
  7717. }
  7718. if (unlikely(!enable_vnmi &&
  7719. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  7720. if (vmx_interrupt_allowed(vcpu)) {
  7721. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  7722. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  7723. vcpu->arch.nmi_pending) {
  7724. /*
  7725. * This CPU don't support us in finding the end of an
  7726. * NMI-blocked window if the guest runs with IRQs
  7727. * disabled. So we pull the trigger after 1 s of
  7728. * futile waiting, but inform the user about this.
  7729. */
  7730. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  7731. "state on VCPU %d after 1 s timeout\n",
  7732. __func__, vcpu->vcpu_id);
  7733. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  7734. }
  7735. }
  7736. if (exit_reason < kvm_vmx_max_exit_handlers
  7737. && kvm_vmx_exit_handlers[exit_reason])
  7738. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  7739. else {
  7740. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  7741. exit_reason);
  7742. kvm_queue_exception(vcpu, UD_VECTOR);
  7743. return 1;
  7744. }
  7745. }
  7746. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  7747. {
  7748. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7749. if (is_guest_mode(vcpu) &&
  7750. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  7751. return;
  7752. if (irr == -1 || tpr < irr) {
  7753. vmcs_write32(TPR_THRESHOLD, 0);
  7754. return;
  7755. }
  7756. vmcs_write32(TPR_THRESHOLD, irr);
  7757. }
  7758. static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
  7759. {
  7760. u32 sec_exec_control;
  7761. /* Postpone execution until vmcs01 is the current VMCS. */
  7762. if (is_guest_mode(vcpu)) {
  7763. to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
  7764. return;
  7765. }
  7766. if (!cpu_has_vmx_virtualize_x2apic_mode())
  7767. return;
  7768. if (!cpu_need_tpr_shadow(vcpu))
  7769. return;
  7770. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  7771. if (set) {
  7772. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  7773. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  7774. } else {
  7775. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  7776. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  7777. vmx_flush_tlb_ept_only(vcpu);
  7778. }
  7779. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  7780. vmx_update_msr_bitmap(vcpu);
  7781. }
  7782. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  7783. {
  7784. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7785. /*
  7786. * Currently we do not handle the nested case where L2 has an
  7787. * APIC access page of its own; that page is still pinned.
  7788. * Hence, we skip the case where the VCPU is in guest mode _and_
  7789. * L1 prepared an APIC access page for L2.
  7790. *
  7791. * For the case where L1 and L2 share the same APIC access page
  7792. * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
  7793. * in the vmcs12), this function will only update either the vmcs01
  7794. * or the vmcs02. If the former, the vmcs02 will be updated by
  7795. * prepare_vmcs02. If the latter, the vmcs01 will be updated in
  7796. * the next L2->L1 exit.
  7797. */
  7798. if (!is_guest_mode(vcpu) ||
  7799. !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
  7800. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  7801. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  7802. vmx_flush_tlb_ept_only(vcpu);
  7803. }
  7804. }
  7805. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  7806. {
  7807. u16 status;
  7808. u8 old;
  7809. if (max_isr == -1)
  7810. max_isr = 0;
  7811. status = vmcs_read16(GUEST_INTR_STATUS);
  7812. old = status >> 8;
  7813. if (max_isr != old) {
  7814. status &= 0xff;
  7815. status |= max_isr << 8;
  7816. vmcs_write16(GUEST_INTR_STATUS, status);
  7817. }
  7818. }
  7819. static void vmx_set_rvi(int vector)
  7820. {
  7821. u16 status;
  7822. u8 old;
  7823. if (vector == -1)
  7824. vector = 0;
  7825. status = vmcs_read16(GUEST_INTR_STATUS);
  7826. old = (u8)status & 0xff;
  7827. if ((u8)vector != old) {
  7828. status &= ~0xff;
  7829. status |= (u8)vector;
  7830. vmcs_write16(GUEST_INTR_STATUS, status);
  7831. }
  7832. }
  7833. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  7834. {
  7835. /*
  7836. * When running L2, updating RVI is only relevant when
  7837. * vmcs12 virtual-interrupt-delivery enabled.
  7838. * However, it can be enabled only when L1 also
  7839. * intercepts external-interrupts and in that case
  7840. * we should not update vmcs02 RVI but instead intercept
  7841. * interrupt. Therefore, do nothing when running L2.
  7842. */
  7843. if (!is_guest_mode(vcpu))
  7844. vmx_set_rvi(max_irr);
  7845. }
  7846. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  7847. {
  7848. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7849. int max_irr;
  7850. bool max_irr_updated;
  7851. WARN_ON(!vcpu->arch.apicv_active);
  7852. if (pi_test_on(&vmx->pi_desc)) {
  7853. pi_clear_on(&vmx->pi_desc);
  7854. /*
  7855. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  7856. * But on x86 this is just a compiler barrier anyway.
  7857. */
  7858. smp_mb__after_atomic();
  7859. max_irr_updated =
  7860. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  7861. /*
  7862. * If we are running L2 and L1 has a new pending interrupt
  7863. * which can be injected, we should re-evaluate
  7864. * what should be done with this new L1 interrupt.
  7865. * If L1 intercepts external-interrupts, we should
  7866. * exit from L2 to L1. Otherwise, interrupt should be
  7867. * delivered directly to L2.
  7868. */
  7869. if (is_guest_mode(vcpu) && max_irr_updated) {
  7870. if (nested_exit_on_intr(vcpu))
  7871. kvm_vcpu_exiting_guest_mode(vcpu);
  7872. else
  7873. kvm_make_request(KVM_REQ_EVENT, vcpu);
  7874. }
  7875. } else {
  7876. max_irr = kvm_lapic_find_highest_irr(vcpu);
  7877. }
  7878. vmx_hwapic_irr_update(vcpu, max_irr);
  7879. return max_irr;
  7880. }
  7881. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  7882. {
  7883. if (!kvm_vcpu_apicv_active(vcpu))
  7884. return;
  7885. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  7886. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  7887. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  7888. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  7889. }
  7890. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  7891. {
  7892. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7893. pi_clear_on(&vmx->pi_desc);
  7894. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  7895. }
  7896. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  7897. {
  7898. u32 exit_intr_info = 0;
  7899. u16 basic_exit_reason = (u16)vmx->exit_reason;
  7900. if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  7901. || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
  7902. return;
  7903. if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  7904. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7905. vmx->exit_intr_info = exit_intr_info;
  7906. /* if exit due to PF check for async PF */
  7907. if (is_page_fault(exit_intr_info))
  7908. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  7909. /* Handle machine checks before interrupts are enabled */
  7910. if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
  7911. is_machine_check(exit_intr_info))
  7912. kvm_machine_check();
  7913. /* We need to handle NMIs before interrupts are enabled */
  7914. if (is_nmi(exit_intr_info)) {
  7915. kvm_before_handle_nmi(&vmx->vcpu);
  7916. asm("int $2");
  7917. kvm_after_handle_nmi(&vmx->vcpu);
  7918. }
  7919. }
  7920. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  7921. {
  7922. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7923. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  7924. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  7925. unsigned int vector;
  7926. unsigned long entry;
  7927. gate_desc *desc;
  7928. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7929. #ifdef CONFIG_X86_64
  7930. unsigned long tmp;
  7931. #endif
  7932. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  7933. desc = (gate_desc *)vmx->host_idt_base + vector;
  7934. entry = gate_offset(desc);
  7935. asm volatile(
  7936. #ifdef CONFIG_X86_64
  7937. "mov %%" _ASM_SP ", %[sp]\n\t"
  7938. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  7939. "push $%c[ss]\n\t"
  7940. "push %[sp]\n\t"
  7941. #endif
  7942. "pushf\n\t"
  7943. __ASM_SIZE(push) " $%c[cs]\n\t"
  7944. CALL_NOSPEC
  7945. :
  7946. #ifdef CONFIG_X86_64
  7947. [sp]"=&r"(tmp),
  7948. #endif
  7949. ASM_CALL_CONSTRAINT
  7950. :
  7951. THUNK_TARGET(entry),
  7952. [ss]"i"(__KERNEL_DS),
  7953. [cs]"i"(__KERNEL_CS)
  7954. );
  7955. }
  7956. }
  7957. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  7958. static bool vmx_has_high_real_mode_segbase(void)
  7959. {
  7960. return enable_unrestricted_guest || emulate_invalid_guest_state;
  7961. }
  7962. static bool vmx_mpx_supported(void)
  7963. {
  7964. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  7965. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  7966. }
  7967. static bool vmx_xsaves_supported(void)
  7968. {
  7969. return vmcs_config.cpu_based_2nd_exec_ctrl &
  7970. SECONDARY_EXEC_XSAVES;
  7971. }
  7972. static bool vmx_umip_emulated(void)
  7973. {
  7974. return vmcs_config.cpu_based_2nd_exec_ctrl &
  7975. SECONDARY_EXEC_DESC;
  7976. }
  7977. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  7978. {
  7979. u32 exit_intr_info;
  7980. bool unblock_nmi;
  7981. u8 vector;
  7982. bool idtv_info_valid;
  7983. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  7984. if (enable_vnmi) {
  7985. if (vmx->loaded_vmcs->nmi_known_unmasked)
  7986. return;
  7987. /*
  7988. * Can't use vmx->exit_intr_info since we're not sure what
  7989. * the exit reason is.
  7990. */
  7991. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  7992. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  7993. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  7994. /*
  7995. * SDM 3: 27.7.1.2 (September 2008)
  7996. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  7997. * a guest IRET fault.
  7998. * SDM 3: 23.2.2 (September 2008)
  7999. * Bit 12 is undefined in any of the following cases:
  8000. * If the VM exit sets the valid bit in the IDT-vectoring
  8001. * information field.
  8002. * If the VM exit is due to a double fault.
  8003. */
  8004. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  8005. vector != DF_VECTOR && !idtv_info_valid)
  8006. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  8007. GUEST_INTR_STATE_NMI);
  8008. else
  8009. vmx->loaded_vmcs->nmi_known_unmasked =
  8010. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  8011. & GUEST_INTR_STATE_NMI);
  8012. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  8013. vmx->loaded_vmcs->vnmi_blocked_time +=
  8014. ktime_to_ns(ktime_sub(ktime_get(),
  8015. vmx->loaded_vmcs->entry_time));
  8016. }
  8017. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  8018. u32 idt_vectoring_info,
  8019. int instr_len_field,
  8020. int error_code_field)
  8021. {
  8022. u8 vector;
  8023. int type;
  8024. bool idtv_info_valid;
  8025. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  8026. vcpu->arch.nmi_injected = false;
  8027. kvm_clear_exception_queue(vcpu);
  8028. kvm_clear_interrupt_queue(vcpu);
  8029. if (!idtv_info_valid)
  8030. return;
  8031. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8032. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  8033. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  8034. switch (type) {
  8035. case INTR_TYPE_NMI_INTR:
  8036. vcpu->arch.nmi_injected = true;
  8037. /*
  8038. * SDM 3: 27.7.1.2 (September 2008)
  8039. * Clear bit "block by NMI" before VM entry if a NMI
  8040. * delivery faulted.
  8041. */
  8042. vmx_set_nmi_mask(vcpu, false);
  8043. break;
  8044. case INTR_TYPE_SOFT_EXCEPTION:
  8045. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8046. /* fall through */
  8047. case INTR_TYPE_HARD_EXCEPTION:
  8048. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  8049. u32 err = vmcs_read32(error_code_field);
  8050. kvm_requeue_exception_e(vcpu, vector, err);
  8051. } else
  8052. kvm_requeue_exception(vcpu, vector);
  8053. break;
  8054. case INTR_TYPE_SOFT_INTR:
  8055. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  8056. /* fall through */
  8057. case INTR_TYPE_EXT_INTR:
  8058. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  8059. break;
  8060. default:
  8061. break;
  8062. }
  8063. }
  8064. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  8065. {
  8066. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  8067. VM_EXIT_INSTRUCTION_LEN,
  8068. IDT_VECTORING_ERROR_CODE);
  8069. }
  8070. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  8071. {
  8072. __vmx_complete_interrupts(vcpu,
  8073. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8074. VM_ENTRY_INSTRUCTION_LEN,
  8075. VM_ENTRY_EXCEPTION_ERROR_CODE);
  8076. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  8077. }
  8078. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  8079. {
  8080. int i, nr_msrs;
  8081. struct perf_guest_switch_msr *msrs;
  8082. msrs = perf_guest_get_msrs(&nr_msrs);
  8083. if (!msrs)
  8084. return;
  8085. for (i = 0; i < nr_msrs; i++)
  8086. if (msrs[i].host == msrs[i].guest)
  8087. clear_atomic_switch_msr(vmx, msrs[i].msr);
  8088. else
  8089. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  8090. msrs[i].host);
  8091. }
  8092. static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
  8093. {
  8094. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8095. u64 tscl;
  8096. u32 delta_tsc;
  8097. if (vmx->hv_deadline_tsc == -1)
  8098. return;
  8099. tscl = rdtsc();
  8100. if (vmx->hv_deadline_tsc > tscl)
  8101. /* sure to be 32 bit only because checked on set_hv_timer */
  8102. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  8103. cpu_preemption_timer_multi);
  8104. else
  8105. delta_tsc = 0;
  8106. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
  8107. }
  8108. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  8109. {
  8110. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8111. unsigned long cr3, cr4;
  8112. /* Record the guest's net vcpu time for enforced NMI injections. */
  8113. if (unlikely(!enable_vnmi &&
  8114. vmx->loaded_vmcs->soft_vnmi_blocked))
  8115. vmx->loaded_vmcs->entry_time = ktime_get();
  8116. /* Don't enter VMX if guest state is invalid, let the exit handler
  8117. start emulation until we arrive back to a valid state */
  8118. if (vmx->emulation_required)
  8119. return;
  8120. if (vmx->ple_window_dirty) {
  8121. vmx->ple_window_dirty = false;
  8122. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  8123. }
  8124. if (vmx->nested.sync_shadow_vmcs) {
  8125. copy_vmcs12_to_shadow(vmx);
  8126. vmx->nested.sync_shadow_vmcs = false;
  8127. }
  8128. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  8129. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  8130. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  8131. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  8132. cr3 = __get_current_cr3_fast();
  8133. if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
  8134. vmcs_writel(HOST_CR3, cr3);
  8135. vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
  8136. }
  8137. cr4 = cr4_read_shadow();
  8138. if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
  8139. vmcs_writel(HOST_CR4, cr4);
  8140. vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
  8141. }
  8142. /* When single-stepping over STI and MOV SS, we must clear the
  8143. * corresponding interruptibility bits in the guest state. Otherwise
  8144. * vmentry fails as it then expects bit 14 (BS) in pending debug
  8145. * exceptions being set, but that's not correct for the guest debugging
  8146. * case. */
  8147. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  8148. vmx_set_interrupt_shadow(vcpu, 0);
  8149. if (static_cpu_has(X86_FEATURE_PKU) &&
  8150. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  8151. vcpu->arch.pkru != vmx->host_pkru)
  8152. __write_pkru(vcpu->arch.pkru);
  8153. atomic_switch_perf_msrs(vmx);
  8154. vmx_arm_hv_timer(vcpu);
  8155. /*
  8156. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  8157. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  8158. * is no need to worry about the conditional branch over the wrmsr
  8159. * being speculatively taken.
  8160. */
  8161. if (vmx->spec_ctrl)
  8162. native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
  8163. vmx->__launched = vmx->loaded_vmcs->launched;
  8164. asm(
  8165. /* Store host registers */
  8166. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  8167. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  8168. "push %%" _ASM_CX " \n\t"
  8169. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8170. "je 1f \n\t"
  8171. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  8172. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  8173. "1: \n\t"
  8174. /* Reload cr2 if changed */
  8175. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  8176. "mov %%cr2, %%" _ASM_DX " \n\t"
  8177. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  8178. "je 2f \n\t"
  8179. "mov %%" _ASM_AX", %%cr2 \n\t"
  8180. "2: \n\t"
  8181. /* Check if vmlaunch of vmresume is needed */
  8182. "cmpl $0, %c[launched](%0) \n\t"
  8183. /* Load guest registers. Don't clobber flags. */
  8184. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  8185. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  8186. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  8187. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  8188. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  8189. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  8190. #ifdef CONFIG_X86_64
  8191. "mov %c[r8](%0), %%r8 \n\t"
  8192. "mov %c[r9](%0), %%r9 \n\t"
  8193. "mov %c[r10](%0), %%r10 \n\t"
  8194. "mov %c[r11](%0), %%r11 \n\t"
  8195. "mov %c[r12](%0), %%r12 \n\t"
  8196. "mov %c[r13](%0), %%r13 \n\t"
  8197. "mov %c[r14](%0), %%r14 \n\t"
  8198. "mov %c[r15](%0), %%r15 \n\t"
  8199. #endif
  8200. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  8201. /* Enter guest mode */
  8202. "jne 1f \n\t"
  8203. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  8204. "jmp 2f \n\t"
  8205. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  8206. "2: "
  8207. /* Save guest registers, load host registers, keep flags */
  8208. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  8209. "pop %0 \n\t"
  8210. "setbe %c[fail](%0)\n\t"
  8211. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  8212. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  8213. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  8214. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  8215. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  8216. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  8217. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  8218. #ifdef CONFIG_X86_64
  8219. "mov %%r8, %c[r8](%0) \n\t"
  8220. "mov %%r9, %c[r9](%0) \n\t"
  8221. "mov %%r10, %c[r10](%0) \n\t"
  8222. "mov %%r11, %c[r11](%0) \n\t"
  8223. "mov %%r12, %c[r12](%0) \n\t"
  8224. "mov %%r13, %c[r13](%0) \n\t"
  8225. "mov %%r14, %c[r14](%0) \n\t"
  8226. "mov %%r15, %c[r15](%0) \n\t"
  8227. "xor %%r8d, %%r8d \n\t"
  8228. "xor %%r9d, %%r9d \n\t"
  8229. "xor %%r10d, %%r10d \n\t"
  8230. "xor %%r11d, %%r11d \n\t"
  8231. "xor %%r12d, %%r12d \n\t"
  8232. "xor %%r13d, %%r13d \n\t"
  8233. "xor %%r14d, %%r14d \n\t"
  8234. "xor %%r15d, %%r15d \n\t"
  8235. #endif
  8236. "mov %%cr2, %%" _ASM_AX " \n\t"
  8237. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  8238. "xor %%eax, %%eax \n\t"
  8239. "xor %%ebx, %%ebx \n\t"
  8240. "xor %%esi, %%esi \n\t"
  8241. "xor %%edi, %%edi \n\t"
  8242. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  8243. ".pushsection .rodata \n\t"
  8244. ".global vmx_return \n\t"
  8245. "vmx_return: " _ASM_PTR " 2b \n\t"
  8246. ".popsection"
  8247. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  8248. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  8249. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  8250. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  8251. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  8252. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  8253. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  8254. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  8255. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  8256. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  8257. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  8258. #ifdef CONFIG_X86_64
  8259. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  8260. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  8261. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  8262. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  8263. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  8264. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  8265. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  8266. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  8267. #endif
  8268. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  8269. [wordsize]"i"(sizeof(ulong))
  8270. : "cc", "memory"
  8271. #ifdef CONFIG_X86_64
  8272. , "rax", "rbx", "rdi", "rsi"
  8273. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  8274. #else
  8275. , "eax", "ebx", "edi", "esi"
  8276. #endif
  8277. );
  8278. /*
  8279. * We do not use IBRS in the kernel. If this vCPU has used the
  8280. * SPEC_CTRL MSR it may have left it on; save the value and
  8281. * turn it off. This is much more efficient than blindly adding
  8282. * it to the atomic save/restore list. Especially as the former
  8283. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  8284. *
  8285. * For non-nested case:
  8286. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  8287. * save it.
  8288. *
  8289. * For nested case:
  8290. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  8291. * save it.
  8292. */
  8293. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  8294. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  8295. if (vmx->spec_ctrl)
  8296. native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
  8297. /* Eliminate branch target predictions from guest mode */
  8298. vmexit_fill_RSB();
  8299. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  8300. if (vmx->host_debugctlmsr)
  8301. update_debugctlmsr(vmx->host_debugctlmsr);
  8302. #ifndef CONFIG_X86_64
  8303. /*
  8304. * The sysexit path does not restore ds/es, so we must set them to
  8305. * a reasonable value ourselves.
  8306. *
  8307. * We can't defer this to vmx_load_host_state() since that function
  8308. * may be executed in interrupt context, which saves and restore segments
  8309. * around it, nullifying its effect.
  8310. */
  8311. loadsegment(ds, __USER_DS);
  8312. loadsegment(es, __USER_DS);
  8313. #endif
  8314. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  8315. | (1 << VCPU_EXREG_RFLAGS)
  8316. | (1 << VCPU_EXREG_PDPTR)
  8317. | (1 << VCPU_EXREG_SEGMENTS)
  8318. | (1 << VCPU_EXREG_CR3));
  8319. vcpu->arch.regs_dirty = 0;
  8320. /*
  8321. * eager fpu is enabled if PKEY is supported and CR4 is switched
  8322. * back on host, so it is safe to read guest PKRU from current
  8323. * XSAVE.
  8324. */
  8325. if (static_cpu_has(X86_FEATURE_PKU) &&
  8326. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  8327. vcpu->arch.pkru = __read_pkru();
  8328. if (vcpu->arch.pkru != vmx->host_pkru)
  8329. __write_pkru(vmx->host_pkru);
  8330. }
  8331. /*
  8332. * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
  8333. * we did not inject a still-pending event to L1 now because of
  8334. * nested_run_pending, we need to re-enable this bit.
  8335. */
  8336. if (vmx->nested.nested_run_pending)
  8337. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8338. vmx->nested.nested_run_pending = 0;
  8339. vmx->idt_vectoring_info = 0;
  8340. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  8341. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  8342. return;
  8343. vmx->loaded_vmcs->launched = 1;
  8344. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  8345. vmx_complete_atomic_exit(vmx);
  8346. vmx_recover_nmi_blocking(vmx);
  8347. vmx_complete_interrupts(vmx);
  8348. }
  8349. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  8350. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  8351. {
  8352. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8353. int cpu;
  8354. if (vmx->loaded_vmcs == vmcs)
  8355. return;
  8356. cpu = get_cpu();
  8357. vmx->loaded_vmcs = vmcs;
  8358. vmx_vcpu_put(vcpu);
  8359. vmx_vcpu_load(vcpu, cpu);
  8360. put_cpu();
  8361. }
  8362. /*
  8363. * Ensure that the current vmcs of the logical processor is the
  8364. * vmcs01 of the vcpu before calling free_nested().
  8365. */
  8366. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  8367. {
  8368. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8369. vcpu_load(vcpu);
  8370. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  8371. free_nested(vmx);
  8372. vcpu_put(vcpu);
  8373. }
  8374. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  8375. {
  8376. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8377. if (enable_pml)
  8378. vmx_destroy_pml_buffer(vmx);
  8379. free_vpid(vmx->vpid);
  8380. leave_guest_mode(vcpu);
  8381. vmx_free_vcpu_nested(vcpu);
  8382. free_loaded_vmcs(vmx->loaded_vmcs);
  8383. kfree(vmx->guest_msrs);
  8384. kvm_vcpu_uninit(vcpu);
  8385. kmem_cache_free(kvm_vcpu_cache, vmx);
  8386. }
  8387. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  8388. {
  8389. int err;
  8390. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  8391. unsigned long *msr_bitmap;
  8392. int cpu;
  8393. if (!vmx)
  8394. return ERR_PTR(-ENOMEM);
  8395. vmx->vpid = allocate_vpid();
  8396. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  8397. if (err)
  8398. goto free_vcpu;
  8399. err = -ENOMEM;
  8400. /*
  8401. * If PML is turned on, failure on enabling PML just results in failure
  8402. * of creating the vcpu, therefore we can simplify PML logic (by
  8403. * avoiding dealing with cases, such as enabling PML partially on vcpus
  8404. * for the guest, etc.
  8405. */
  8406. if (enable_pml) {
  8407. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  8408. if (!vmx->pml_pg)
  8409. goto uninit_vcpu;
  8410. }
  8411. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  8412. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  8413. > PAGE_SIZE);
  8414. if (!vmx->guest_msrs)
  8415. goto free_pml;
  8416. err = alloc_loaded_vmcs(&vmx->vmcs01);
  8417. if (err < 0)
  8418. goto free_msrs;
  8419. msr_bitmap = vmx->vmcs01.msr_bitmap;
  8420. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  8421. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  8422. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  8423. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  8424. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  8425. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  8426. vmx->msr_bitmap_mode = 0;
  8427. vmx->loaded_vmcs = &vmx->vmcs01;
  8428. cpu = get_cpu();
  8429. vmx_vcpu_load(&vmx->vcpu, cpu);
  8430. vmx->vcpu.cpu = cpu;
  8431. vmx_vcpu_setup(vmx);
  8432. vmx_vcpu_put(&vmx->vcpu);
  8433. put_cpu();
  8434. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  8435. err = alloc_apic_access_page(kvm);
  8436. if (err)
  8437. goto free_vmcs;
  8438. }
  8439. if (enable_ept) {
  8440. err = init_rmode_identity_map(kvm);
  8441. if (err)
  8442. goto free_vmcs;
  8443. }
  8444. if (nested) {
  8445. nested_vmx_setup_ctls_msrs(vmx);
  8446. vmx->nested.vpid02 = allocate_vpid();
  8447. }
  8448. vmx->nested.posted_intr_nv = -1;
  8449. vmx->nested.current_vmptr = -1ull;
  8450. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  8451. /*
  8452. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  8453. * or POSTED_INTR_WAKEUP_VECTOR.
  8454. */
  8455. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  8456. vmx->pi_desc.sn = 1;
  8457. return &vmx->vcpu;
  8458. free_vmcs:
  8459. free_vpid(vmx->nested.vpid02);
  8460. free_loaded_vmcs(vmx->loaded_vmcs);
  8461. free_msrs:
  8462. kfree(vmx->guest_msrs);
  8463. free_pml:
  8464. vmx_destroy_pml_buffer(vmx);
  8465. uninit_vcpu:
  8466. kvm_vcpu_uninit(&vmx->vcpu);
  8467. free_vcpu:
  8468. free_vpid(vmx->vpid);
  8469. kmem_cache_free(kvm_vcpu_cache, vmx);
  8470. return ERR_PTR(err);
  8471. }
  8472. static void __init vmx_check_processor_compat(void *rtn)
  8473. {
  8474. struct vmcs_config vmcs_conf;
  8475. *(int *)rtn = 0;
  8476. if (setup_vmcs_config(&vmcs_conf) < 0)
  8477. *(int *)rtn = -EIO;
  8478. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  8479. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  8480. smp_processor_id());
  8481. *(int *)rtn = -EIO;
  8482. }
  8483. }
  8484. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  8485. {
  8486. u8 cache;
  8487. u64 ipat = 0;
  8488. /* For VT-d and EPT combination
  8489. * 1. MMIO: always map as UC
  8490. * 2. EPT with VT-d:
  8491. * a. VT-d without snooping control feature: can't guarantee the
  8492. * result, try to trust guest.
  8493. * b. VT-d with snooping control feature: snooping control feature of
  8494. * VT-d engine can guarantee the cache correctness. Just set it
  8495. * to WB to keep consistent with host. So the same as item 3.
  8496. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  8497. * consistent with host MTRR
  8498. */
  8499. if (is_mmio) {
  8500. cache = MTRR_TYPE_UNCACHABLE;
  8501. goto exit;
  8502. }
  8503. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  8504. ipat = VMX_EPT_IPAT_BIT;
  8505. cache = MTRR_TYPE_WRBACK;
  8506. goto exit;
  8507. }
  8508. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  8509. ipat = VMX_EPT_IPAT_BIT;
  8510. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  8511. cache = MTRR_TYPE_WRBACK;
  8512. else
  8513. cache = MTRR_TYPE_UNCACHABLE;
  8514. goto exit;
  8515. }
  8516. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  8517. exit:
  8518. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  8519. }
  8520. static int vmx_get_lpage_level(void)
  8521. {
  8522. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  8523. return PT_DIRECTORY_LEVEL;
  8524. else
  8525. /* For shadow and EPT supported 1GB page */
  8526. return PT_PDPE_LEVEL;
  8527. }
  8528. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  8529. {
  8530. /*
  8531. * These bits in the secondary execution controls field
  8532. * are dynamic, the others are mostly based on the hypervisor
  8533. * architecture and the guest's CPUID. Do not touch the
  8534. * dynamic bits.
  8535. */
  8536. u32 mask =
  8537. SECONDARY_EXEC_SHADOW_VMCS |
  8538. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  8539. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8540. SECONDARY_EXEC_DESC;
  8541. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8542. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  8543. (new_ctl & ~mask) | (cur_ctl & mask));
  8544. }
  8545. /*
  8546. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  8547. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  8548. */
  8549. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  8550. {
  8551. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8552. struct kvm_cpuid_entry2 *entry;
  8553. vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
  8554. vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
  8555. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  8556. if (entry && (entry->_reg & (_cpuid_mask))) \
  8557. vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
  8558. } while (0)
  8559. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  8560. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  8561. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  8562. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  8563. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  8564. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  8565. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  8566. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  8567. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  8568. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  8569. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  8570. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  8571. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  8572. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  8573. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  8574. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  8575. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  8576. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  8577. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  8578. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  8579. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  8580. #undef cr4_fixed1_update
  8581. }
  8582. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  8583. {
  8584. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8585. if (cpu_has_secondary_exec_ctrls()) {
  8586. vmx_compute_secondary_exec_control(vmx);
  8587. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  8588. }
  8589. if (nested_vmx_allowed(vcpu))
  8590. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  8591. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  8592. else
  8593. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  8594. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  8595. if (nested_vmx_allowed(vcpu))
  8596. nested_vmx_cr_fixed1_bits_update(vcpu);
  8597. }
  8598. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  8599. {
  8600. if (func == 1 && nested)
  8601. entry->ecx |= bit(X86_FEATURE_VMX);
  8602. }
  8603. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  8604. struct x86_exception *fault)
  8605. {
  8606. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8607. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8608. u32 exit_reason;
  8609. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  8610. if (vmx->nested.pml_full) {
  8611. exit_reason = EXIT_REASON_PML_FULL;
  8612. vmx->nested.pml_full = false;
  8613. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  8614. } else if (fault->error_code & PFERR_RSVD_MASK)
  8615. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  8616. else
  8617. exit_reason = EXIT_REASON_EPT_VIOLATION;
  8618. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  8619. vmcs12->guest_physical_address = fault->address;
  8620. }
  8621. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  8622. {
  8623. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  8624. }
  8625. /* Callbacks for nested_ept_init_mmu_context: */
  8626. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  8627. {
  8628. /* return the page table to be shadowed - in our case, EPT12 */
  8629. return get_vmcs12(vcpu)->ept_pointer;
  8630. }
  8631. static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  8632. {
  8633. WARN_ON(mmu_is_nested(vcpu));
  8634. if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
  8635. return 1;
  8636. kvm_mmu_unload(vcpu);
  8637. kvm_init_shadow_ept_mmu(vcpu,
  8638. to_vmx(vcpu)->nested.nested_vmx_ept_caps &
  8639. VMX_EPT_EXECUTE_ONLY_BIT,
  8640. nested_ept_ad_enabled(vcpu));
  8641. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  8642. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  8643. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  8644. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  8645. return 0;
  8646. }
  8647. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  8648. {
  8649. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  8650. }
  8651. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  8652. u16 error_code)
  8653. {
  8654. bool inequality, bit;
  8655. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  8656. inequality =
  8657. (error_code & vmcs12->page_fault_error_code_mask) !=
  8658. vmcs12->page_fault_error_code_match;
  8659. return inequality ^ bit;
  8660. }
  8661. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  8662. struct x86_exception *fault)
  8663. {
  8664. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8665. WARN_ON(!is_guest_mode(vcpu));
  8666. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  8667. !to_vmx(vcpu)->nested.nested_run_pending) {
  8668. vmcs12->vm_exit_intr_error_code = fault->error_code;
  8669. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  8670. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  8671. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  8672. fault->address);
  8673. } else {
  8674. kvm_inject_page_fault(vcpu, fault);
  8675. }
  8676. }
  8677. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  8678. struct vmcs12 *vmcs12);
  8679. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
  8680. struct vmcs12 *vmcs12)
  8681. {
  8682. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8683. struct page *page;
  8684. u64 hpa;
  8685. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  8686. /*
  8687. * Translate L1 physical address to host physical
  8688. * address for vmcs02. Keep the page pinned, so this
  8689. * physical address remains valid. We keep a reference
  8690. * to it so we can release it later.
  8691. */
  8692. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  8693. kvm_release_page_dirty(vmx->nested.apic_access_page);
  8694. vmx->nested.apic_access_page = NULL;
  8695. }
  8696. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  8697. /*
  8698. * If translation failed, no matter: This feature asks
  8699. * to exit when accessing the given address, and if it
  8700. * can never be accessed, this feature won't do
  8701. * anything anyway.
  8702. */
  8703. if (!is_error_page(page)) {
  8704. vmx->nested.apic_access_page = page;
  8705. hpa = page_to_phys(vmx->nested.apic_access_page);
  8706. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  8707. } else {
  8708. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  8709. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  8710. }
  8711. } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
  8712. cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  8713. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  8714. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  8715. kvm_vcpu_reload_apic_access_page(vcpu);
  8716. }
  8717. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  8718. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  8719. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  8720. vmx->nested.virtual_apic_page = NULL;
  8721. }
  8722. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  8723. /*
  8724. * If translation failed, VM entry will fail because
  8725. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  8726. * Failing the vm entry is _not_ what the processor
  8727. * does but it's basically the only possibility we
  8728. * have. We could still enter the guest if CR8 load
  8729. * exits are enabled, CR8 store exits are enabled, and
  8730. * virtualize APIC access is disabled; in this case
  8731. * the processor would never use the TPR shadow and we
  8732. * could simply clear the bit from the execution
  8733. * control. But such a configuration is useless, so
  8734. * let's keep the code simple.
  8735. */
  8736. if (!is_error_page(page)) {
  8737. vmx->nested.virtual_apic_page = page;
  8738. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  8739. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  8740. }
  8741. }
  8742. if (nested_cpu_has_posted_intr(vmcs12)) {
  8743. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  8744. kunmap(vmx->nested.pi_desc_page);
  8745. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  8746. vmx->nested.pi_desc_page = NULL;
  8747. }
  8748. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  8749. if (is_error_page(page))
  8750. return;
  8751. vmx->nested.pi_desc_page = page;
  8752. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  8753. vmx->nested.pi_desc =
  8754. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  8755. (unsigned long)(vmcs12->posted_intr_desc_addr &
  8756. (PAGE_SIZE - 1)));
  8757. vmcs_write64(POSTED_INTR_DESC_ADDR,
  8758. page_to_phys(vmx->nested.pi_desc_page) +
  8759. (unsigned long)(vmcs12->posted_intr_desc_addr &
  8760. (PAGE_SIZE - 1)));
  8761. }
  8762. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  8763. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  8764. CPU_BASED_USE_MSR_BITMAPS);
  8765. else
  8766. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  8767. CPU_BASED_USE_MSR_BITMAPS);
  8768. }
  8769. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  8770. {
  8771. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  8772. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8773. if (vcpu->arch.virtual_tsc_khz == 0)
  8774. return;
  8775. /* Make sure short timeouts reliably trigger an immediate vmexit.
  8776. * hrtimer_start does not guarantee this. */
  8777. if (preemption_timeout <= 1) {
  8778. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  8779. return;
  8780. }
  8781. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  8782. preemption_timeout *= 1000000;
  8783. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  8784. hrtimer_start(&vmx->nested.preemption_timer,
  8785. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  8786. }
  8787. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  8788. struct vmcs12 *vmcs12)
  8789. {
  8790. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  8791. return 0;
  8792. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  8793. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  8794. return -EINVAL;
  8795. return 0;
  8796. }
  8797. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  8798. struct vmcs12 *vmcs12)
  8799. {
  8800. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8801. return 0;
  8802. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  8803. return -EINVAL;
  8804. return 0;
  8805. }
  8806. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  8807. struct vmcs12 *vmcs12)
  8808. {
  8809. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8810. return 0;
  8811. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  8812. return -EINVAL;
  8813. return 0;
  8814. }
  8815. /*
  8816. * Merge L0's and L1's MSR bitmap, return false to indicate that
  8817. * we do not use the hardware.
  8818. */
  8819. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  8820. struct vmcs12 *vmcs12)
  8821. {
  8822. int msr;
  8823. struct page *page;
  8824. unsigned long *msr_bitmap_l1;
  8825. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  8826. /*
  8827. * pred_cmd & spec_ctrl are trying to verify two things:
  8828. *
  8829. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  8830. * ensures that we do not accidentally generate an L02 MSR bitmap
  8831. * from the L12 MSR bitmap that is too permissive.
  8832. * 2. That L1 or L2s have actually used the MSR. This avoids
  8833. * unnecessarily merging of the bitmap if the MSR is unused. This
  8834. * works properly because we only update the L01 MSR bitmap lazily.
  8835. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  8836. * updated to reflect this when L1 (or its L2s) actually write to
  8837. * the MSR.
  8838. */
  8839. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  8840. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  8841. /* Nothing to do if the MSR bitmap is not in use. */
  8842. if (!cpu_has_vmx_msr_bitmap() ||
  8843. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8844. return false;
  8845. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  8846. !pred_cmd && !spec_ctrl)
  8847. return false;
  8848. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  8849. if (is_error_page(page))
  8850. return false;
  8851. msr_bitmap_l1 = (unsigned long *)kmap(page);
  8852. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  8853. /*
  8854. * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
  8855. * just lets the processor take the value from the virtual-APIC page;
  8856. * take those 256 bits directly from the L1 bitmap.
  8857. */
  8858. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  8859. unsigned word = msr / BITS_PER_LONG;
  8860. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  8861. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  8862. }
  8863. } else {
  8864. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  8865. unsigned word = msr / BITS_PER_LONG;
  8866. msr_bitmap_l0[word] = ~0;
  8867. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  8868. }
  8869. }
  8870. nested_vmx_disable_intercept_for_msr(
  8871. msr_bitmap_l1, msr_bitmap_l0,
  8872. X2APIC_MSR(APIC_TASKPRI),
  8873. MSR_TYPE_W);
  8874. if (nested_cpu_has_vid(vmcs12)) {
  8875. nested_vmx_disable_intercept_for_msr(
  8876. msr_bitmap_l1, msr_bitmap_l0,
  8877. X2APIC_MSR(APIC_EOI),
  8878. MSR_TYPE_W);
  8879. nested_vmx_disable_intercept_for_msr(
  8880. msr_bitmap_l1, msr_bitmap_l0,
  8881. X2APIC_MSR(APIC_SELF_IPI),
  8882. MSR_TYPE_W);
  8883. }
  8884. if (spec_ctrl)
  8885. nested_vmx_disable_intercept_for_msr(
  8886. msr_bitmap_l1, msr_bitmap_l0,
  8887. MSR_IA32_SPEC_CTRL,
  8888. MSR_TYPE_R | MSR_TYPE_W);
  8889. if (pred_cmd)
  8890. nested_vmx_disable_intercept_for_msr(
  8891. msr_bitmap_l1, msr_bitmap_l0,
  8892. MSR_IA32_PRED_CMD,
  8893. MSR_TYPE_W);
  8894. kunmap(page);
  8895. kvm_release_page_clean(page);
  8896. return true;
  8897. }
  8898. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  8899. struct vmcs12 *vmcs12)
  8900. {
  8901. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  8902. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  8903. !nested_cpu_has_vid(vmcs12) &&
  8904. !nested_cpu_has_posted_intr(vmcs12))
  8905. return 0;
  8906. /*
  8907. * If virtualize x2apic mode is enabled,
  8908. * virtualize apic access must be disabled.
  8909. */
  8910. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  8911. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  8912. return -EINVAL;
  8913. /*
  8914. * If virtual interrupt delivery is enabled,
  8915. * we must exit on external interrupts.
  8916. */
  8917. if (nested_cpu_has_vid(vmcs12) &&
  8918. !nested_exit_on_intr(vcpu))
  8919. return -EINVAL;
  8920. /*
  8921. * bits 15:8 should be zero in posted_intr_nv,
  8922. * the descriptor address has been already checked
  8923. * in nested_get_vmcs12_pages.
  8924. */
  8925. if (nested_cpu_has_posted_intr(vmcs12) &&
  8926. (!nested_cpu_has_vid(vmcs12) ||
  8927. !nested_exit_intr_ack_set(vcpu) ||
  8928. vmcs12->posted_intr_nv & 0xff00))
  8929. return -EINVAL;
  8930. /* tpr shadow is needed by all apicv features. */
  8931. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8932. return -EINVAL;
  8933. return 0;
  8934. }
  8935. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  8936. unsigned long count_field,
  8937. unsigned long addr_field)
  8938. {
  8939. int maxphyaddr;
  8940. u64 count, addr;
  8941. if (vmcs12_read_any(vcpu, count_field, &count) ||
  8942. vmcs12_read_any(vcpu, addr_field, &addr)) {
  8943. WARN_ON(1);
  8944. return -EINVAL;
  8945. }
  8946. if (count == 0)
  8947. return 0;
  8948. maxphyaddr = cpuid_maxphyaddr(vcpu);
  8949. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  8950. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  8951. pr_debug_ratelimited(
  8952. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  8953. addr_field, maxphyaddr, count, addr);
  8954. return -EINVAL;
  8955. }
  8956. return 0;
  8957. }
  8958. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  8959. struct vmcs12 *vmcs12)
  8960. {
  8961. if (vmcs12->vm_exit_msr_load_count == 0 &&
  8962. vmcs12->vm_exit_msr_store_count == 0 &&
  8963. vmcs12->vm_entry_msr_load_count == 0)
  8964. return 0; /* Fast path */
  8965. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  8966. VM_EXIT_MSR_LOAD_ADDR) ||
  8967. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  8968. VM_EXIT_MSR_STORE_ADDR) ||
  8969. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  8970. VM_ENTRY_MSR_LOAD_ADDR))
  8971. return -EINVAL;
  8972. return 0;
  8973. }
  8974. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  8975. struct vmcs12 *vmcs12)
  8976. {
  8977. u64 address = vmcs12->pml_address;
  8978. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  8979. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
  8980. if (!nested_cpu_has_ept(vmcs12) ||
  8981. !IS_ALIGNED(address, 4096) ||
  8982. address >> maxphyaddr)
  8983. return -EINVAL;
  8984. }
  8985. return 0;
  8986. }
  8987. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  8988. struct vmx_msr_entry *e)
  8989. {
  8990. /* x2APIC MSR accesses are not allowed */
  8991. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  8992. return -EINVAL;
  8993. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  8994. e->index == MSR_IA32_UCODE_REV)
  8995. return -EINVAL;
  8996. if (e->reserved != 0)
  8997. return -EINVAL;
  8998. return 0;
  8999. }
  9000. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  9001. struct vmx_msr_entry *e)
  9002. {
  9003. if (e->index == MSR_FS_BASE ||
  9004. e->index == MSR_GS_BASE ||
  9005. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  9006. nested_vmx_msr_check_common(vcpu, e))
  9007. return -EINVAL;
  9008. return 0;
  9009. }
  9010. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  9011. struct vmx_msr_entry *e)
  9012. {
  9013. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  9014. nested_vmx_msr_check_common(vcpu, e))
  9015. return -EINVAL;
  9016. return 0;
  9017. }
  9018. /*
  9019. * Load guest's/host's msr at nested entry/exit.
  9020. * return 0 for success, entry index for failure.
  9021. */
  9022. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9023. {
  9024. u32 i;
  9025. struct vmx_msr_entry e;
  9026. struct msr_data msr;
  9027. msr.host_initiated = false;
  9028. for (i = 0; i < count; i++) {
  9029. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  9030. &e, sizeof(e))) {
  9031. pr_debug_ratelimited(
  9032. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9033. __func__, i, gpa + i * sizeof(e));
  9034. goto fail;
  9035. }
  9036. if (nested_vmx_load_msr_check(vcpu, &e)) {
  9037. pr_debug_ratelimited(
  9038. "%s check failed (%u, 0x%x, 0x%x)\n",
  9039. __func__, i, e.index, e.reserved);
  9040. goto fail;
  9041. }
  9042. msr.index = e.index;
  9043. msr.data = e.value;
  9044. if (kvm_set_msr(vcpu, &msr)) {
  9045. pr_debug_ratelimited(
  9046. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9047. __func__, i, e.index, e.value);
  9048. goto fail;
  9049. }
  9050. }
  9051. return 0;
  9052. fail:
  9053. return i + 1;
  9054. }
  9055. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  9056. {
  9057. u32 i;
  9058. struct vmx_msr_entry e;
  9059. for (i = 0; i < count; i++) {
  9060. struct msr_data msr_info;
  9061. if (kvm_vcpu_read_guest(vcpu,
  9062. gpa + i * sizeof(e),
  9063. &e, 2 * sizeof(u32))) {
  9064. pr_debug_ratelimited(
  9065. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  9066. __func__, i, gpa + i * sizeof(e));
  9067. return -EINVAL;
  9068. }
  9069. if (nested_vmx_store_msr_check(vcpu, &e)) {
  9070. pr_debug_ratelimited(
  9071. "%s check failed (%u, 0x%x, 0x%x)\n",
  9072. __func__, i, e.index, e.reserved);
  9073. return -EINVAL;
  9074. }
  9075. msr_info.host_initiated = false;
  9076. msr_info.index = e.index;
  9077. if (kvm_get_msr(vcpu, &msr_info)) {
  9078. pr_debug_ratelimited(
  9079. "%s cannot read MSR (%u, 0x%x)\n",
  9080. __func__, i, e.index);
  9081. return -EINVAL;
  9082. }
  9083. if (kvm_vcpu_write_guest(vcpu,
  9084. gpa + i * sizeof(e) +
  9085. offsetof(struct vmx_msr_entry, value),
  9086. &msr_info.data, sizeof(msr_info.data))) {
  9087. pr_debug_ratelimited(
  9088. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  9089. __func__, i, e.index, msr_info.data);
  9090. return -EINVAL;
  9091. }
  9092. }
  9093. return 0;
  9094. }
  9095. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  9096. {
  9097. unsigned long invalid_mask;
  9098. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  9099. return (val & invalid_mask) == 0;
  9100. }
  9101. /*
  9102. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  9103. * emulating VM entry into a guest with EPT enabled.
  9104. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9105. * is assigned to entry_failure_code on failure.
  9106. */
  9107. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  9108. u32 *entry_failure_code)
  9109. {
  9110. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  9111. if (!nested_cr3_valid(vcpu, cr3)) {
  9112. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9113. return 1;
  9114. }
  9115. /*
  9116. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  9117. * must not be dereferenced.
  9118. */
  9119. if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
  9120. !nested_ept) {
  9121. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  9122. *entry_failure_code = ENTRY_FAIL_PDPTE;
  9123. return 1;
  9124. }
  9125. }
  9126. vcpu->arch.cr3 = cr3;
  9127. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  9128. }
  9129. kvm_mmu_reset_context(vcpu);
  9130. return 0;
  9131. }
  9132. static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9133. bool from_vmentry)
  9134. {
  9135. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9136. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  9137. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  9138. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  9139. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  9140. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  9141. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  9142. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  9143. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  9144. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  9145. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  9146. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  9147. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  9148. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  9149. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  9150. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  9151. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  9152. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  9153. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  9154. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  9155. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  9156. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  9157. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  9158. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  9159. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  9160. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  9161. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  9162. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  9163. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  9164. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  9165. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  9166. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  9167. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  9168. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  9169. vmcs12->guest_pending_dbg_exceptions);
  9170. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  9171. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  9172. if (nested_cpu_has_xsaves(vmcs12))
  9173. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  9174. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  9175. if (cpu_has_vmx_posted_intr())
  9176. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  9177. /*
  9178. * Whether page-faults are trapped is determined by a combination of
  9179. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  9180. * If enable_ept, L0 doesn't care about page faults and we should
  9181. * set all of these to L1's desires. However, if !enable_ept, L0 does
  9182. * care about (at least some) page faults, and because it is not easy
  9183. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  9184. * to exit on each and every L2 page fault. This is done by setting
  9185. * MASK=MATCH=0 and (see below) EB.PF=1.
  9186. * Note that below we don't need special code to set EB.PF beyond the
  9187. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  9188. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  9189. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  9190. */
  9191. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  9192. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  9193. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  9194. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  9195. /* All VMFUNCs are currently emulated through L0 vmexits. */
  9196. if (cpu_has_vmx_vmfunc())
  9197. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  9198. if (cpu_has_vmx_apicv()) {
  9199. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  9200. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  9201. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  9202. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  9203. }
  9204. /*
  9205. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  9206. * Some constant fields are set here by vmx_set_constant_host_state().
  9207. * Other fields are different per CPU, and will be set later when
  9208. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  9209. */
  9210. vmx_set_constant_host_state(vmx);
  9211. /*
  9212. * Set the MSR load/store lists to match L0's settings.
  9213. */
  9214. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  9215. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9216. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  9217. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  9218. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  9219. set_cr4_guest_host_mask(vmx);
  9220. if (vmx_mpx_supported())
  9221. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  9222. if (enable_vpid) {
  9223. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  9224. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  9225. else
  9226. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  9227. }
  9228. /*
  9229. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  9230. */
  9231. if (enable_ept) {
  9232. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  9233. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  9234. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  9235. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  9236. }
  9237. if (cpu_has_vmx_msr_bitmap())
  9238. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  9239. }
  9240. /*
  9241. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  9242. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  9243. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  9244. * guest in a way that will both be appropriate to L1's requests, and our
  9245. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  9246. * function also has additional necessary side-effects, like setting various
  9247. * vcpu->arch fields.
  9248. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  9249. * is assigned to entry_failure_code on failure.
  9250. */
  9251. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9252. bool from_vmentry, u32 *entry_failure_code)
  9253. {
  9254. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9255. u32 exec_control, vmcs12_exec_ctrl;
  9256. /*
  9257. * First, the fields that are shadowed. This must be kept in sync
  9258. * with vmx_shadow_fields.h.
  9259. */
  9260. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  9261. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  9262. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  9263. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  9264. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  9265. /*
  9266. * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
  9267. * HOST_FS_BASE, HOST_GS_BASE.
  9268. */
  9269. if (from_vmentry &&
  9270. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  9271. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  9272. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  9273. } else {
  9274. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  9275. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  9276. }
  9277. if (from_vmentry) {
  9278. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  9279. vmcs12->vm_entry_intr_info_field);
  9280. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  9281. vmcs12->vm_entry_exception_error_code);
  9282. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  9283. vmcs12->vm_entry_instruction_len);
  9284. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  9285. vmcs12->guest_interruptibility_info);
  9286. vmx->loaded_vmcs->nmi_known_unmasked =
  9287. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  9288. } else {
  9289. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9290. }
  9291. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  9292. exec_control = vmcs12->pin_based_vm_exec_control;
  9293. /* Preemption timer setting is only taken from vmcs01. */
  9294. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9295. exec_control |= vmcs_config.pin_based_exec_ctrl;
  9296. if (vmx->hv_deadline_tsc == -1)
  9297. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  9298. /* Posted interrupts setting is only taken from vmcs12. */
  9299. if (nested_cpu_has_posted_intr(vmcs12)) {
  9300. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  9301. vmx->nested.pi_pending = false;
  9302. } else {
  9303. exec_control &= ~PIN_BASED_POSTED_INTR;
  9304. }
  9305. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  9306. vmx->nested.preemption_timer_expired = false;
  9307. if (nested_cpu_has_preemption_timer(vmcs12))
  9308. vmx_start_preemption_timer(vcpu);
  9309. if (cpu_has_secondary_exec_ctrls()) {
  9310. exec_control = vmx->secondary_exec_control;
  9311. /* Take the following fields only from vmcs12 */
  9312. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9313. SECONDARY_EXEC_ENABLE_INVPCID |
  9314. SECONDARY_EXEC_RDTSCP |
  9315. SECONDARY_EXEC_XSAVES |
  9316. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  9317. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  9318. SECONDARY_EXEC_ENABLE_VMFUNC);
  9319. if (nested_cpu_has(vmcs12,
  9320. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  9321. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  9322. ~SECONDARY_EXEC_ENABLE_PML;
  9323. exec_control |= vmcs12_exec_ctrl;
  9324. }
  9325. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  9326. vmcs_write16(GUEST_INTR_STATUS,
  9327. vmcs12->guest_intr_status);
  9328. /*
  9329. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  9330. * nested_get_vmcs12_pages will either fix it up or
  9331. * remove the VM execution control.
  9332. */
  9333. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  9334. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  9335. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  9336. }
  9337. /*
  9338. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  9339. * entry, but only if the current (host) sp changed from the value
  9340. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  9341. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  9342. * here we just force the write to happen on entry.
  9343. */
  9344. vmx->host_rsp = 0;
  9345. exec_control = vmx_exec_control(vmx); /* L0's desires */
  9346. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  9347. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  9348. exec_control &= ~CPU_BASED_TPR_SHADOW;
  9349. exec_control |= vmcs12->cpu_based_vm_exec_control;
  9350. /*
  9351. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  9352. * nested_get_vmcs12_pages can't fix it up, the illegal value
  9353. * will result in a VM entry failure.
  9354. */
  9355. if (exec_control & CPU_BASED_TPR_SHADOW) {
  9356. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  9357. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  9358. } else {
  9359. #ifdef CONFIG_X86_64
  9360. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  9361. CPU_BASED_CR8_STORE_EXITING;
  9362. #endif
  9363. }
  9364. /*
  9365. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  9366. * for I/O port accesses.
  9367. */
  9368. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  9369. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  9370. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  9371. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  9372. * bitwise-or of what L1 wants to trap for L2, and what we want to
  9373. * trap. Note that CR0.TS also needs updating - we do this later.
  9374. */
  9375. update_exception_bitmap(vcpu);
  9376. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  9377. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  9378. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  9379. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  9380. * bits are further modified by vmx_set_efer() below.
  9381. */
  9382. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  9383. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  9384. * emulated by vmx_set_efer(), below.
  9385. */
  9386. vm_entry_controls_init(vmx,
  9387. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  9388. ~VM_ENTRY_IA32E_MODE) |
  9389. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  9390. if (from_vmentry &&
  9391. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  9392. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  9393. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  9394. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  9395. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  9396. }
  9397. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  9398. vmcs_write64(TSC_OFFSET,
  9399. vcpu->arch.tsc_offset + vmcs12->tsc_offset);
  9400. else
  9401. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  9402. if (kvm_has_tsc_control)
  9403. decache_tsc_multiplier(vmx);
  9404. if (enable_vpid) {
  9405. /*
  9406. * There is no direct mapping between vpid02 and vpid12, the
  9407. * vpid02 is per-vCPU for L0 and reused while the value of
  9408. * vpid12 is changed w/ one invvpid during nested vmentry.
  9409. * The vpid12 is allocated by L1 for L2, so it will not
  9410. * influence global bitmap(for vpid01 and vpid02 allocation)
  9411. * even if spawn a lot of nested vCPUs.
  9412. */
  9413. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
  9414. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  9415. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  9416. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
  9417. }
  9418. } else {
  9419. vmx_flush_tlb(vcpu, true);
  9420. }
  9421. }
  9422. if (enable_pml) {
  9423. /*
  9424. * Conceptually we want to copy the PML address and index from
  9425. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  9426. * since we always flush the log on each vmexit, this happens
  9427. * to be equivalent to simply resetting the fields in vmcs02.
  9428. */
  9429. ASSERT(vmx->pml_pg);
  9430. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  9431. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  9432. }
  9433. if (nested_cpu_has_ept(vmcs12)) {
  9434. if (nested_ept_init_mmu_context(vcpu)) {
  9435. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  9436. return 1;
  9437. }
  9438. } else if (nested_cpu_has2(vmcs12,
  9439. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9440. vmx_flush_tlb_ept_only(vcpu);
  9441. }
  9442. /*
  9443. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  9444. * bits which we consider mandatory enabled.
  9445. * The CR0_READ_SHADOW is what L2 should have expected to read given
  9446. * the specifications by L1; It's not enough to take
  9447. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  9448. * have more bits than L1 expected.
  9449. */
  9450. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  9451. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  9452. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  9453. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  9454. if (from_vmentry &&
  9455. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  9456. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  9457. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  9458. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  9459. else
  9460. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  9461. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  9462. vmx_set_efer(vcpu, vcpu->arch.efer);
  9463. if (vmx->nested.dirty_vmcs12) {
  9464. prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
  9465. vmx->nested.dirty_vmcs12 = false;
  9466. }
  9467. /* Shadow page tables on either EPT or shadow page tables. */
  9468. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  9469. entry_failure_code))
  9470. return 1;
  9471. if (!enable_ept)
  9472. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  9473. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  9474. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  9475. return 0;
  9476. }
  9477. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9478. {
  9479. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9480. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  9481. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  9482. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9483. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  9484. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9485. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  9486. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9487. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  9488. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9489. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  9490. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9491. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  9492. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9493. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  9494. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9495. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  9496. vmx->nested.nested_vmx_procbased_ctls_low,
  9497. vmx->nested.nested_vmx_procbased_ctls_high) ||
  9498. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  9499. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  9500. vmx->nested.nested_vmx_secondary_ctls_low,
  9501. vmx->nested.nested_vmx_secondary_ctls_high)) ||
  9502. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  9503. vmx->nested.nested_vmx_pinbased_ctls_low,
  9504. vmx->nested.nested_vmx_pinbased_ctls_high) ||
  9505. !vmx_control_verify(vmcs12->vm_exit_controls,
  9506. vmx->nested.nested_vmx_exit_ctls_low,
  9507. vmx->nested.nested_vmx_exit_ctls_high) ||
  9508. !vmx_control_verify(vmcs12->vm_entry_controls,
  9509. vmx->nested.nested_vmx_entry_ctls_low,
  9510. vmx->nested.nested_vmx_entry_ctls_high))
  9511. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9512. if (nested_cpu_has_vmfunc(vmcs12)) {
  9513. if (vmcs12->vm_function_control &
  9514. ~vmx->nested.nested_vmx_vmfunc_controls)
  9515. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9516. if (nested_cpu_has_eptp_switching(vmcs12)) {
  9517. if (!nested_cpu_has_ept(vmcs12) ||
  9518. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  9519. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9520. }
  9521. }
  9522. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  9523. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  9524. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  9525. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  9526. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  9527. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  9528. return 0;
  9529. }
  9530. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9531. u32 *exit_qual)
  9532. {
  9533. bool ia32e;
  9534. *exit_qual = ENTRY_FAIL_DEFAULT;
  9535. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  9536. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  9537. return 1;
  9538. if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
  9539. vmcs12->vmcs_link_pointer != -1ull) {
  9540. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  9541. return 1;
  9542. }
  9543. /*
  9544. * If the load IA32_EFER VM-entry control is 1, the following checks
  9545. * are performed on the field for the IA32_EFER MSR:
  9546. * - Bits reserved in the IA32_EFER MSR must be 0.
  9547. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  9548. * the IA-32e mode guest VM-exit control. It must also be identical
  9549. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  9550. * CR0.PG) is 1.
  9551. */
  9552. if (to_vmx(vcpu)->nested.nested_run_pending &&
  9553. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  9554. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  9555. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  9556. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  9557. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  9558. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  9559. return 1;
  9560. }
  9561. /*
  9562. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  9563. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  9564. * the values of the LMA and LME bits in the field must each be that of
  9565. * the host address-space size VM-exit control.
  9566. */
  9567. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  9568. ia32e = (vmcs12->vm_exit_controls &
  9569. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  9570. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  9571. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  9572. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  9573. return 1;
  9574. }
  9575. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  9576. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  9577. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  9578. return 1;
  9579. return 0;
  9580. }
  9581. static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
  9582. {
  9583. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9584. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9585. u32 msr_entry_idx;
  9586. u32 exit_qual;
  9587. enter_guest_mode(vcpu);
  9588. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  9589. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  9590. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  9591. vmx_segment_cache_clear(vmx);
  9592. if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
  9593. leave_guest_mode(vcpu);
  9594. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  9595. nested_vmx_entry_failure(vcpu, vmcs12,
  9596. EXIT_REASON_INVALID_STATE, exit_qual);
  9597. return 1;
  9598. }
  9599. nested_get_vmcs12_pages(vcpu, vmcs12);
  9600. msr_entry_idx = nested_vmx_load_msr(vcpu,
  9601. vmcs12->vm_entry_msr_load_addr,
  9602. vmcs12->vm_entry_msr_load_count);
  9603. if (msr_entry_idx) {
  9604. leave_guest_mode(vcpu);
  9605. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  9606. nested_vmx_entry_failure(vcpu, vmcs12,
  9607. EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
  9608. return 1;
  9609. }
  9610. /*
  9611. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  9612. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  9613. * returned as far as L1 is concerned. It will only return (and set
  9614. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  9615. */
  9616. return 0;
  9617. }
  9618. /*
  9619. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  9620. * for running an L2 nested guest.
  9621. */
  9622. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  9623. {
  9624. struct vmcs12 *vmcs12;
  9625. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9626. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  9627. u32 exit_qual;
  9628. int ret;
  9629. if (!nested_vmx_check_permission(vcpu))
  9630. return 1;
  9631. if (!nested_vmx_check_vmcs12(vcpu))
  9632. goto out;
  9633. vmcs12 = get_vmcs12(vcpu);
  9634. if (enable_shadow_vmcs)
  9635. copy_shadow_to_vmcs12(vmx);
  9636. /*
  9637. * The nested entry process starts with enforcing various prerequisites
  9638. * on vmcs12 as required by the Intel SDM, and act appropriately when
  9639. * they fail: As the SDM explains, some conditions should cause the
  9640. * instruction to fail, while others will cause the instruction to seem
  9641. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  9642. * To speed up the normal (success) code path, we should avoid checking
  9643. * for misconfigurations which will anyway be caught by the processor
  9644. * when using the merged vmcs02.
  9645. */
  9646. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
  9647. nested_vmx_failValid(vcpu,
  9648. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  9649. goto out;
  9650. }
  9651. if (vmcs12->launch_state == launch) {
  9652. nested_vmx_failValid(vcpu,
  9653. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  9654. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  9655. goto out;
  9656. }
  9657. ret = check_vmentry_prereqs(vcpu, vmcs12);
  9658. if (ret) {
  9659. nested_vmx_failValid(vcpu, ret);
  9660. goto out;
  9661. }
  9662. /*
  9663. * After this point, the trap flag no longer triggers a singlestep trap
  9664. * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
  9665. * This is not 100% correct; for performance reasons, we delegate most
  9666. * of the checks on host state to the processor. If those fail,
  9667. * the singlestep trap is missed.
  9668. */
  9669. skip_emulated_instruction(vcpu);
  9670. ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
  9671. if (ret) {
  9672. nested_vmx_entry_failure(vcpu, vmcs12,
  9673. EXIT_REASON_INVALID_STATE, exit_qual);
  9674. return 1;
  9675. }
  9676. /*
  9677. * We're finally done with prerequisite checking, and can start with
  9678. * the nested entry.
  9679. */
  9680. ret = enter_vmx_non_root_mode(vcpu, true);
  9681. if (ret)
  9682. return ret;
  9683. /*
  9684. * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
  9685. * by event injection, halt vcpu.
  9686. */
  9687. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  9688. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
  9689. return kvm_vcpu_halt(vcpu);
  9690. vmx->nested.nested_run_pending = 1;
  9691. return 1;
  9692. out:
  9693. return kvm_skip_emulated_instruction(vcpu);
  9694. }
  9695. /*
  9696. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  9697. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  9698. * This function returns the new value we should put in vmcs12.guest_cr0.
  9699. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  9700. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  9701. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  9702. * didn't trap the bit, because if L1 did, so would L0).
  9703. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  9704. * been modified by L2, and L1 knows it. So just leave the old value of
  9705. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  9706. * isn't relevant, because if L0 traps this bit it can set it to anything.
  9707. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  9708. * changed these bits, and therefore they need to be updated, but L0
  9709. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  9710. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  9711. */
  9712. static inline unsigned long
  9713. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9714. {
  9715. return
  9716. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  9717. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  9718. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  9719. vcpu->arch.cr0_guest_owned_bits));
  9720. }
  9721. static inline unsigned long
  9722. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9723. {
  9724. return
  9725. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  9726. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  9727. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  9728. vcpu->arch.cr4_guest_owned_bits));
  9729. }
  9730. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  9731. struct vmcs12 *vmcs12)
  9732. {
  9733. u32 idt_vectoring;
  9734. unsigned int nr;
  9735. if (vcpu->arch.exception.injected) {
  9736. nr = vcpu->arch.exception.nr;
  9737. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  9738. if (kvm_exception_is_soft(nr)) {
  9739. vmcs12->vm_exit_instruction_len =
  9740. vcpu->arch.event_exit_inst_len;
  9741. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  9742. } else
  9743. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  9744. if (vcpu->arch.exception.has_error_code) {
  9745. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  9746. vmcs12->idt_vectoring_error_code =
  9747. vcpu->arch.exception.error_code;
  9748. }
  9749. vmcs12->idt_vectoring_info_field = idt_vectoring;
  9750. } else if (vcpu->arch.nmi_injected) {
  9751. vmcs12->idt_vectoring_info_field =
  9752. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  9753. } else if (vcpu->arch.interrupt.pending) {
  9754. nr = vcpu->arch.interrupt.nr;
  9755. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  9756. if (vcpu->arch.interrupt.soft) {
  9757. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  9758. vmcs12->vm_entry_instruction_len =
  9759. vcpu->arch.event_exit_inst_len;
  9760. } else
  9761. idt_vectoring |= INTR_TYPE_EXT_INTR;
  9762. vmcs12->idt_vectoring_info_field = idt_vectoring;
  9763. }
  9764. }
  9765. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  9766. {
  9767. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9768. unsigned long exit_qual;
  9769. bool block_nested_events =
  9770. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  9771. if (vcpu->arch.exception.pending &&
  9772. nested_vmx_check_exception(vcpu, &exit_qual)) {
  9773. if (block_nested_events)
  9774. return -EBUSY;
  9775. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  9776. return 0;
  9777. }
  9778. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  9779. vmx->nested.preemption_timer_expired) {
  9780. if (block_nested_events)
  9781. return -EBUSY;
  9782. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  9783. return 0;
  9784. }
  9785. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  9786. if (block_nested_events)
  9787. return -EBUSY;
  9788. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  9789. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  9790. INTR_INFO_VALID_MASK, 0);
  9791. /*
  9792. * The NMI-triggered VM exit counts as injection:
  9793. * clear this one and block further NMIs.
  9794. */
  9795. vcpu->arch.nmi_pending = 0;
  9796. vmx_set_nmi_mask(vcpu, true);
  9797. return 0;
  9798. }
  9799. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  9800. nested_exit_on_intr(vcpu)) {
  9801. if (block_nested_events)
  9802. return -EBUSY;
  9803. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  9804. return 0;
  9805. }
  9806. vmx_complete_nested_posted_interrupt(vcpu);
  9807. return 0;
  9808. }
  9809. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  9810. {
  9811. ktime_t remaining =
  9812. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  9813. u64 value;
  9814. if (ktime_to_ns(remaining) <= 0)
  9815. return 0;
  9816. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  9817. do_div(value, 1000000);
  9818. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  9819. }
  9820. /*
  9821. * Update the guest state fields of vmcs12 to reflect changes that
  9822. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  9823. * VM-entry controls is also updated, since this is really a guest
  9824. * state bit.)
  9825. */
  9826. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  9827. {
  9828. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  9829. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  9830. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  9831. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  9832. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  9833. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  9834. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  9835. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  9836. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  9837. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  9838. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  9839. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  9840. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  9841. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  9842. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  9843. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  9844. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  9845. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  9846. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  9847. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  9848. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  9849. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  9850. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  9851. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  9852. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  9853. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  9854. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  9855. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  9856. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  9857. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  9858. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  9859. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  9860. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  9861. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  9862. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  9863. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  9864. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  9865. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  9866. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  9867. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  9868. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  9869. vmcs12->guest_interruptibility_info =
  9870. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  9871. vmcs12->guest_pending_dbg_exceptions =
  9872. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  9873. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  9874. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  9875. else
  9876. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  9877. if (nested_cpu_has_preemption_timer(vmcs12)) {
  9878. if (vmcs12->vm_exit_controls &
  9879. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  9880. vmcs12->vmx_preemption_timer_value =
  9881. vmx_get_preemption_timer_value(vcpu);
  9882. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  9883. }
  9884. /*
  9885. * In some cases (usually, nested EPT), L2 is allowed to change its
  9886. * own CR3 without exiting. If it has changed it, we must keep it.
  9887. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  9888. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  9889. *
  9890. * Additionally, restore L2's PDPTR to vmcs12.
  9891. */
  9892. if (enable_ept) {
  9893. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  9894. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  9895. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  9896. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  9897. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  9898. }
  9899. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  9900. if (nested_cpu_has_vid(vmcs12))
  9901. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  9902. vmcs12->vm_entry_controls =
  9903. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  9904. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  9905. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  9906. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  9907. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  9908. }
  9909. /* TODO: These cannot have changed unless we have MSR bitmaps and
  9910. * the relevant bit asks not to trap the change */
  9911. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  9912. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  9913. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  9914. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  9915. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  9916. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  9917. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  9918. if (kvm_mpx_supported())
  9919. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  9920. }
  9921. /*
  9922. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  9923. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  9924. * and this function updates it to reflect the changes to the guest state while
  9925. * L2 was running (and perhaps made some exits which were handled directly by L0
  9926. * without going back to L1), and to reflect the exit reason.
  9927. * Note that we do not have to copy here all VMCS fields, just those that
  9928. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  9929. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  9930. * which already writes to vmcs12 directly.
  9931. */
  9932. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  9933. u32 exit_reason, u32 exit_intr_info,
  9934. unsigned long exit_qualification)
  9935. {
  9936. /* update guest state fields: */
  9937. sync_vmcs12(vcpu, vmcs12);
  9938. /* update exit information fields: */
  9939. vmcs12->vm_exit_reason = exit_reason;
  9940. vmcs12->exit_qualification = exit_qualification;
  9941. vmcs12->vm_exit_intr_info = exit_intr_info;
  9942. vmcs12->idt_vectoring_info_field = 0;
  9943. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  9944. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  9945. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  9946. vmcs12->launch_state = 1;
  9947. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  9948. * instead of reading the real value. */
  9949. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  9950. /*
  9951. * Transfer the event that L0 or L1 may wanted to inject into
  9952. * L2 to IDT_VECTORING_INFO_FIELD.
  9953. */
  9954. vmcs12_save_pending_event(vcpu, vmcs12);
  9955. }
  9956. /*
  9957. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  9958. * preserved above and would only end up incorrectly in L1.
  9959. */
  9960. vcpu->arch.nmi_injected = false;
  9961. kvm_clear_exception_queue(vcpu);
  9962. kvm_clear_interrupt_queue(vcpu);
  9963. }
  9964. static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
  9965. struct vmcs12 *vmcs12)
  9966. {
  9967. u32 entry_failure_code;
  9968. nested_ept_uninit_mmu_context(vcpu);
  9969. /*
  9970. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  9971. * couldn't have changed.
  9972. */
  9973. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  9974. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  9975. if (!enable_ept)
  9976. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  9977. }
  9978. /*
  9979. * A part of what we need to when the nested L2 guest exits and we want to
  9980. * run its L1 parent, is to reset L1's guest state to the host state specified
  9981. * in vmcs12.
  9982. * This function is to be called not only on normal nested exit, but also on
  9983. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  9984. * Failures During or After Loading Guest State").
  9985. * This function should be called when the active VMCS is L1's (vmcs01).
  9986. */
  9987. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  9988. struct vmcs12 *vmcs12)
  9989. {
  9990. struct kvm_segment seg;
  9991. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  9992. vcpu->arch.efer = vmcs12->host_ia32_efer;
  9993. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  9994. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  9995. else
  9996. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  9997. vmx_set_efer(vcpu, vcpu->arch.efer);
  9998. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  9999. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  10000. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  10001. /*
  10002. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  10003. * actually changed, because vmx_set_cr0 refers to efer set above.
  10004. *
  10005. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  10006. * (KVM doesn't change it);
  10007. */
  10008. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  10009. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  10010. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  10011. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  10012. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  10013. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10014. if (enable_vpid) {
  10015. /*
  10016. * Trivially support vpid by letting L2s share their parent
  10017. * L1's vpid. TODO: move to a more elaborate solution, giving
  10018. * each L2 its own vpid and exposing the vpid feature to L1.
  10019. */
  10020. vmx_flush_tlb(vcpu, true);
  10021. }
  10022. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  10023. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  10024. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  10025. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  10026. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  10027. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  10028. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  10029. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  10030. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  10031. vmcs_write64(GUEST_BNDCFGS, 0);
  10032. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  10033. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  10034. vcpu->arch.pat = vmcs12->host_ia32_pat;
  10035. }
  10036. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  10037. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  10038. vmcs12->host_ia32_perf_global_ctrl);
  10039. /* Set L1 segment info according to Intel SDM
  10040. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  10041. seg = (struct kvm_segment) {
  10042. .base = 0,
  10043. .limit = 0xFFFFFFFF,
  10044. .selector = vmcs12->host_cs_selector,
  10045. .type = 11,
  10046. .present = 1,
  10047. .s = 1,
  10048. .g = 1
  10049. };
  10050. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  10051. seg.l = 1;
  10052. else
  10053. seg.db = 1;
  10054. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  10055. seg = (struct kvm_segment) {
  10056. .base = 0,
  10057. .limit = 0xFFFFFFFF,
  10058. .type = 3,
  10059. .present = 1,
  10060. .s = 1,
  10061. .db = 1,
  10062. .g = 1
  10063. };
  10064. seg.selector = vmcs12->host_ds_selector;
  10065. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  10066. seg.selector = vmcs12->host_es_selector;
  10067. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  10068. seg.selector = vmcs12->host_ss_selector;
  10069. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  10070. seg.selector = vmcs12->host_fs_selector;
  10071. seg.base = vmcs12->host_fs_base;
  10072. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  10073. seg.selector = vmcs12->host_gs_selector;
  10074. seg.base = vmcs12->host_gs_base;
  10075. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  10076. seg = (struct kvm_segment) {
  10077. .base = vmcs12->host_tr_base,
  10078. .limit = 0x67,
  10079. .selector = vmcs12->host_tr_selector,
  10080. .type = 11,
  10081. .present = 1
  10082. };
  10083. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  10084. kvm_set_dr(vcpu, 7, 0x400);
  10085. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  10086. if (cpu_has_vmx_msr_bitmap())
  10087. vmx_update_msr_bitmap(vcpu);
  10088. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  10089. vmcs12->vm_exit_msr_load_count))
  10090. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  10091. }
  10092. /*
  10093. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  10094. * and modify vmcs12 to make it see what it would expect to see there if
  10095. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  10096. */
  10097. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  10098. u32 exit_intr_info,
  10099. unsigned long exit_qualification)
  10100. {
  10101. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10102. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10103. /* trying to cancel vmlaunch/vmresume is a bug */
  10104. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  10105. /*
  10106. * The only expected VM-instruction error is "VM entry with
  10107. * invalid control field(s)." Anything else indicates a
  10108. * problem with L0.
  10109. */
  10110. WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
  10111. VMXERR_ENTRY_INVALID_CONTROL_FIELD));
  10112. leave_guest_mode(vcpu);
  10113. if (likely(!vmx->fail)) {
  10114. if (exit_reason == -1)
  10115. sync_vmcs12(vcpu, vmcs12);
  10116. else
  10117. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  10118. exit_qualification);
  10119. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  10120. vmcs12->vm_exit_msr_store_count))
  10121. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  10122. }
  10123. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  10124. vm_entry_controls_reset_shadow(vmx);
  10125. vm_exit_controls_reset_shadow(vmx);
  10126. vmx_segment_cache_clear(vmx);
  10127. /* Update any VMCS fields that might have changed while L2 ran */
  10128. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10129. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
  10130. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10131. if (vmx->hv_deadline_tsc == -1)
  10132. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10133. PIN_BASED_VMX_PREEMPTION_TIMER);
  10134. else
  10135. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10136. PIN_BASED_VMX_PREEMPTION_TIMER);
  10137. if (kvm_has_tsc_control)
  10138. decache_tsc_multiplier(vmx);
  10139. if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
  10140. vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
  10141. vmx_set_virtual_x2apic_mode(vcpu,
  10142. vcpu->arch.apic_base & X2APIC_ENABLE);
  10143. } else if (!nested_cpu_has_ept(vmcs12) &&
  10144. nested_cpu_has2(vmcs12,
  10145. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10146. vmx_flush_tlb_ept_only(vcpu);
  10147. }
  10148. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  10149. vmx->host_rsp = 0;
  10150. /* Unpin physical memory we referred to in vmcs02 */
  10151. if (vmx->nested.apic_access_page) {
  10152. kvm_release_page_dirty(vmx->nested.apic_access_page);
  10153. vmx->nested.apic_access_page = NULL;
  10154. }
  10155. if (vmx->nested.virtual_apic_page) {
  10156. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  10157. vmx->nested.virtual_apic_page = NULL;
  10158. }
  10159. if (vmx->nested.pi_desc_page) {
  10160. kunmap(vmx->nested.pi_desc_page);
  10161. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  10162. vmx->nested.pi_desc_page = NULL;
  10163. vmx->nested.pi_desc = NULL;
  10164. }
  10165. /*
  10166. * We are now running in L2, mmu_notifier will force to reload the
  10167. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  10168. */
  10169. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  10170. if (enable_shadow_vmcs && exit_reason != -1)
  10171. vmx->nested.sync_shadow_vmcs = true;
  10172. /* in case we halted in L2 */
  10173. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  10174. if (likely(!vmx->fail)) {
  10175. /*
  10176. * TODO: SDM says that with acknowledge interrupt on
  10177. * exit, bit 31 of the VM-exit interrupt information
  10178. * (valid interrupt) is always set to 1 on
  10179. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  10180. * need kvm_cpu_has_interrupt(). See the commit
  10181. * message for details.
  10182. */
  10183. if (nested_exit_intr_ack_set(vcpu) &&
  10184. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  10185. kvm_cpu_has_interrupt(vcpu)) {
  10186. int irq = kvm_cpu_get_interrupt(vcpu);
  10187. WARN_ON(irq < 0);
  10188. vmcs12->vm_exit_intr_info = irq |
  10189. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  10190. }
  10191. if (exit_reason != -1)
  10192. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  10193. vmcs12->exit_qualification,
  10194. vmcs12->idt_vectoring_info_field,
  10195. vmcs12->vm_exit_intr_info,
  10196. vmcs12->vm_exit_intr_error_code,
  10197. KVM_ISA_VMX);
  10198. load_vmcs12_host_state(vcpu, vmcs12);
  10199. return;
  10200. }
  10201. /*
  10202. * After an early L2 VM-entry failure, we're now back
  10203. * in L1 which thinks it just finished a VMLAUNCH or
  10204. * VMRESUME instruction, so we need to set the failure
  10205. * flag and the VM-instruction error field of the VMCS
  10206. * accordingly.
  10207. */
  10208. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  10209. load_vmcs12_mmu_host_state(vcpu, vmcs12);
  10210. /*
  10211. * The emulated instruction was already skipped in
  10212. * nested_vmx_run, but the updated RIP was never
  10213. * written back to the vmcs01.
  10214. */
  10215. skip_emulated_instruction(vcpu);
  10216. vmx->fail = 0;
  10217. }
  10218. /*
  10219. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  10220. */
  10221. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  10222. {
  10223. if (is_guest_mode(vcpu)) {
  10224. to_vmx(vcpu)->nested.nested_run_pending = 0;
  10225. nested_vmx_vmexit(vcpu, -1, 0, 0);
  10226. }
  10227. free_nested(to_vmx(vcpu));
  10228. }
  10229. /*
  10230. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  10231. * 23.7 "VM-entry failures during or after loading guest state" (this also
  10232. * lists the acceptable exit-reason and exit-qualification parameters).
  10233. * It should only be called before L2 actually succeeded to run, and when
  10234. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  10235. */
  10236. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  10237. struct vmcs12 *vmcs12,
  10238. u32 reason, unsigned long qualification)
  10239. {
  10240. load_vmcs12_host_state(vcpu, vmcs12);
  10241. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  10242. vmcs12->exit_qualification = qualification;
  10243. nested_vmx_succeed(vcpu);
  10244. if (enable_shadow_vmcs)
  10245. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  10246. }
  10247. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  10248. struct x86_instruction_info *info,
  10249. enum x86_intercept_stage stage)
  10250. {
  10251. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10252. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  10253. /*
  10254. * RDPID causes #UD if disabled through secondary execution controls.
  10255. * Because it is marked as EmulateOnUD, we need to intercept it here.
  10256. */
  10257. if (info->intercept == x86_intercept_rdtscp &&
  10258. !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  10259. ctxt->exception.vector = UD_VECTOR;
  10260. ctxt->exception.error_code_valid = false;
  10261. return X86EMUL_PROPAGATE_FAULT;
  10262. }
  10263. /* TODO: check more intercepts... */
  10264. return X86EMUL_CONTINUE;
  10265. }
  10266. #ifdef CONFIG_X86_64
  10267. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  10268. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  10269. u64 divisor, u64 *result)
  10270. {
  10271. u64 low = a << shift, high = a >> (64 - shift);
  10272. /* To avoid the overflow on divq */
  10273. if (high >= divisor)
  10274. return 1;
  10275. /* Low hold the result, high hold rem which is discarded */
  10276. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  10277. "rm" (divisor), "0" (low), "1" (high));
  10278. *result = low;
  10279. return 0;
  10280. }
  10281. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  10282. {
  10283. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10284. u64 tscl = rdtsc();
  10285. u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  10286. u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  10287. /* Convert to host delta tsc if tsc scaling is enabled */
  10288. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  10289. u64_shl_div_u64(delta_tsc,
  10290. kvm_tsc_scaling_ratio_frac_bits,
  10291. vcpu->arch.tsc_scaling_ratio,
  10292. &delta_tsc))
  10293. return -ERANGE;
  10294. /*
  10295. * If the delta tsc can't fit in the 32 bit after the multi shift,
  10296. * we can't use the preemption timer.
  10297. * It's possible that it fits on later vmentries, but checking
  10298. * on every vmentry is costly so we just use an hrtimer.
  10299. */
  10300. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  10301. return -ERANGE;
  10302. vmx->hv_deadline_tsc = tscl + delta_tsc;
  10303. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  10304. PIN_BASED_VMX_PREEMPTION_TIMER);
  10305. return delta_tsc == 0;
  10306. }
  10307. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  10308. {
  10309. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10310. vmx->hv_deadline_tsc = -1;
  10311. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  10312. PIN_BASED_VMX_PREEMPTION_TIMER);
  10313. }
  10314. #endif
  10315. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  10316. {
  10317. if (ple_gap)
  10318. shrink_ple_window(vcpu);
  10319. }
  10320. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  10321. struct kvm_memory_slot *slot)
  10322. {
  10323. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  10324. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  10325. }
  10326. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  10327. struct kvm_memory_slot *slot)
  10328. {
  10329. kvm_mmu_slot_set_dirty(kvm, slot);
  10330. }
  10331. static void vmx_flush_log_dirty(struct kvm *kvm)
  10332. {
  10333. kvm_flush_pml_buffers(kvm);
  10334. }
  10335. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  10336. {
  10337. struct vmcs12 *vmcs12;
  10338. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10339. gpa_t gpa;
  10340. struct page *page = NULL;
  10341. u64 *pml_address;
  10342. if (is_guest_mode(vcpu)) {
  10343. WARN_ON_ONCE(vmx->nested.pml_full);
  10344. /*
  10345. * Check if PML is enabled for the nested guest.
  10346. * Whether eptp bit 6 is set is already checked
  10347. * as part of A/D emulation.
  10348. */
  10349. vmcs12 = get_vmcs12(vcpu);
  10350. if (!nested_cpu_has_pml(vmcs12))
  10351. return 0;
  10352. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  10353. vmx->nested.pml_full = true;
  10354. return 1;
  10355. }
  10356. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  10357. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  10358. if (is_error_page(page))
  10359. return 0;
  10360. pml_address = kmap(page);
  10361. pml_address[vmcs12->guest_pml_index--] = gpa;
  10362. kunmap(page);
  10363. kvm_release_page_clean(page);
  10364. }
  10365. return 0;
  10366. }
  10367. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  10368. struct kvm_memory_slot *memslot,
  10369. gfn_t offset, unsigned long mask)
  10370. {
  10371. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  10372. }
  10373. static void __pi_post_block(struct kvm_vcpu *vcpu)
  10374. {
  10375. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  10376. struct pi_desc old, new;
  10377. unsigned int dest;
  10378. do {
  10379. old.control = new.control = pi_desc->control;
  10380. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  10381. "Wakeup handler not enabled while the VCPU is blocked\n");
  10382. dest = cpu_physical_id(vcpu->cpu);
  10383. if (x2apic_enabled())
  10384. new.ndst = dest;
  10385. else
  10386. new.ndst = (dest << 8) & 0xFF00;
  10387. /* set 'NV' to 'notification vector' */
  10388. new.nv = POSTED_INTR_VECTOR;
  10389. } while (cmpxchg64(&pi_desc->control, old.control,
  10390. new.control) != old.control);
  10391. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  10392. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10393. list_del(&vcpu->blocked_vcpu_list);
  10394. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10395. vcpu->pre_pcpu = -1;
  10396. }
  10397. }
  10398. /*
  10399. * This routine does the following things for vCPU which is going
  10400. * to be blocked if VT-d PI is enabled.
  10401. * - Store the vCPU to the wakeup list, so when interrupts happen
  10402. * we can find the right vCPU to wake up.
  10403. * - Change the Posted-interrupt descriptor as below:
  10404. * 'NDST' <-- vcpu->pre_pcpu
  10405. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  10406. * - If 'ON' is set during this process, which means at least one
  10407. * interrupt is posted for this vCPU, we cannot block it, in
  10408. * this case, return 1, otherwise, return 0.
  10409. *
  10410. */
  10411. static int pi_pre_block(struct kvm_vcpu *vcpu)
  10412. {
  10413. unsigned int dest;
  10414. struct pi_desc old, new;
  10415. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  10416. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  10417. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  10418. !kvm_vcpu_apicv_active(vcpu))
  10419. return 0;
  10420. WARN_ON(irqs_disabled());
  10421. local_irq_disable();
  10422. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  10423. vcpu->pre_pcpu = vcpu->cpu;
  10424. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10425. list_add_tail(&vcpu->blocked_vcpu_list,
  10426. &per_cpu(blocked_vcpu_on_cpu,
  10427. vcpu->pre_pcpu));
  10428. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  10429. }
  10430. do {
  10431. old.control = new.control = pi_desc->control;
  10432. WARN((pi_desc->sn == 1),
  10433. "Warning: SN field of posted-interrupts "
  10434. "is set before blocking\n");
  10435. /*
  10436. * Since vCPU can be preempted during this process,
  10437. * vcpu->cpu could be different with pre_pcpu, we
  10438. * need to set pre_pcpu as the destination of wakeup
  10439. * notification event, then we can find the right vCPU
  10440. * to wakeup in wakeup handler if interrupts happen
  10441. * when the vCPU is in blocked state.
  10442. */
  10443. dest = cpu_physical_id(vcpu->pre_pcpu);
  10444. if (x2apic_enabled())
  10445. new.ndst = dest;
  10446. else
  10447. new.ndst = (dest << 8) & 0xFF00;
  10448. /* set 'NV' to 'wakeup vector' */
  10449. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  10450. } while (cmpxchg64(&pi_desc->control, old.control,
  10451. new.control) != old.control);
  10452. /* We should not block the vCPU if an interrupt is posted for it. */
  10453. if (pi_test_on(pi_desc) == 1)
  10454. __pi_post_block(vcpu);
  10455. local_irq_enable();
  10456. return (vcpu->pre_pcpu == -1);
  10457. }
  10458. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  10459. {
  10460. if (pi_pre_block(vcpu))
  10461. return 1;
  10462. if (kvm_lapic_hv_timer_in_use(vcpu))
  10463. kvm_lapic_switch_to_sw_timer(vcpu);
  10464. return 0;
  10465. }
  10466. static void pi_post_block(struct kvm_vcpu *vcpu)
  10467. {
  10468. if (vcpu->pre_pcpu == -1)
  10469. return;
  10470. WARN_ON(irqs_disabled());
  10471. local_irq_disable();
  10472. __pi_post_block(vcpu);
  10473. local_irq_enable();
  10474. }
  10475. static void vmx_post_block(struct kvm_vcpu *vcpu)
  10476. {
  10477. if (kvm_x86_ops->set_hv_timer)
  10478. kvm_lapic_switch_to_hv_timer(vcpu);
  10479. pi_post_block(vcpu);
  10480. }
  10481. /*
  10482. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  10483. *
  10484. * @kvm: kvm
  10485. * @host_irq: host irq of the interrupt
  10486. * @guest_irq: gsi of the interrupt
  10487. * @set: set or unset PI
  10488. * returns 0 on success, < 0 on failure
  10489. */
  10490. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  10491. uint32_t guest_irq, bool set)
  10492. {
  10493. struct kvm_kernel_irq_routing_entry *e;
  10494. struct kvm_irq_routing_table *irq_rt;
  10495. struct kvm_lapic_irq irq;
  10496. struct kvm_vcpu *vcpu;
  10497. struct vcpu_data vcpu_info;
  10498. int idx, ret = 0;
  10499. if (!kvm_arch_has_assigned_device(kvm) ||
  10500. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  10501. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  10502. return 0;
  10503. idx = srcu_read_lock(&kvm->irq_srcu);
  10504. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  10505. if (guest_irq >= irq_rt->nr_rt_entries ||
  10506. hlist_empty(&irq_rt->map[guest_irq])) {
  10507. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  10508. guest_irq, irq_rt->nr_rt_entries);
  10509. goto out;
  10510. }
  10511. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  10512. if (e->type != KVM_IRQ_ROUTING_MSI)
  10513. continue;
  10514. /*
  10515. * VT-d PI cannot support posting multicast/broadcast
  10516. * interrupts to a vCPU, we still use interrupt remapping
  10517. * for these kind of interrupts.
  10518. *
  10519. * For lowest-priority interrupts, we only support
  10520. * those with single CPU as the destination, e.g. user
  10521. * configures the interrupts via /proc/irq or uses
  10522. * irqbalance to make the interrupts single-CPU.
  10523. *
  10524. * We will support full lowest-priority interrupt later.
  10525. */
  10526. kvm_set_msi_irq(kvm, e, &irq);
  10527. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  10528. /*
  10529. * Make sure the IRTE is in remapped mode if
  10530. * we don't handle it in posted mode.
  10531. */
  10532. ret = irq_set_vcpu_affinity(host_irq, NULL);
  10533. if (ret < 0) {
  10534. printk(KERN_INFO
  10535. "failed to back to remapped mode, irq: %u\n",
  10536. host_irq);
  10537. goto out;
  10538. }
  10539. continue;
  10540. }
  10541. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  10542. vcpu_info.vector = irq.vector;
  10543. trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
  10544. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  10545. if (set)
  10546. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  10547. else
  10548. ret = irq_set_vcpu_affinity(host_irq, NULL);
  10549. if (ret < 0) {
  10550. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  10551. __func__);
  10552. goto out;
  10553. }
  10554. }
  10555. ret = 0;
  10556. out:
  10557. srcu_read_unlock(&kvm->irq_srcu, idx);
  10558. return ret;
  10559. }
  10560. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  10561. {
  10562. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  10563. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  10564. FEATURE_CONTROL_LMCE;
  10565. else
  10566. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  10567. ~FEATURE_CONTROL_LMCE;
  10568. }
  10569. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  10570. {
  10571. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  10572. if (to_vmx(vcpu)->nested.nested_run_pending)
  10573. return 0;
  10574. return 1;
  10575. }
  10576. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  10577. {
  10578. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10579. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  10580. if (vmx->nested.smm.guest_mode)
  10581. nested_vmx_vmexit(vcpu, -1, 0, 0);
  10582. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  10583. vmx->nested.vmxon = false;
  10584. return 0;
  10585. }
  10586. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  10587. {
  10588. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10589. int ret;
  10590. if (vmx->nested.smm.vmxon) {
  10591. vmx->nested.vmxon = true;
  10592. vmx->nested.smm.vmxon = false;
  10593. }
  10594. if (vmx->nested.smm.guest_mode) {
  10595. vcpu->arch.hflags &= ~HF_SMM_MASK;
  10596. ret = enter_vmx_non_root_mode(vcpu, false);
  10597. vcpu->arch.hflags |= HF_SMM_MASK;
  10598. if (ret)
  10599. return ret;
  10600. vmx->nested.smm.guest_mode = false;
  10601. }
  10602. return 0;
  10603. }
  10604. static int enable_smi_window(struct kvm_vcpu *vcpu)
  10605. {
  10606. return 0;
  10607. }
  10608. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  10609. .cpu_has_kvm_support = cpu_has_kvm_support,
  10610. .disabled_by_bios = vmx_disabled_by_bios,
  10611. .hardware_setup = hardware_setup,
  10612. .hardware_unsetup = hardware_unsetup,
  10613. .check_processor_compatibility = vmx_check_processor_compat,
  10614. .hardware_enable = hardware_enable,
  10615. .hardware_disable = hardware_disable,
  10616. .cpu_has_accelerated_tpr = report_flexpriority,
  10617. .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
  10618. .vcpu_create = vmx_create_vcpu,
  10619. .vcpu_free = vmx_free_vcpu,
  10620. .vcpu_reset = vmx_vcpu_reset,
  10621. .prepare_guest_switch = vmx_save_host_state,
  10622. .vcpu_load = vmx_vcpu_load,
  10623. .vcpu_put = vmx_vcpu_put,
  10624. .update_bp_intercept = update_exception_bitmap,
  10625. .get_msr_feature = vmx_get_msr_feature,
  10626. .get_msr = vmx_get_msr,
  10627. .set_msr = vmx_set_msr,
  10628. .get_segment_base = vmx_get_segment_base,
  10629. .get_segment = vmx_get_segment,
  10630. .set_segment = vmx_set_segment,
  10631. .get_cpl = vmx_get_cpl,
  10632. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  10633. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  10634. .decache_cr3 = vmx_decache_cr3,
  10635. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  10636. .set_cr0 = vmx_set_cr0,
  10637. .set_cr3 = vmx_set_cr3,
  10638. .set_cr4 = vmx_set_cr4,
  10639. .set_efer = vmx_set_efer,
  10640. .get_idt = vmx_get_idt,
  10641. .set_idt = vmx_set_idt,
  10642. .get_gdt = vmx_get_gdt,
  10643. .set_gdt = vmx_set_gdt,
  10644. .get_dr6 = vmx_get_dr6,
  10645. .set_dr6 = vmx_set_dr6,
  10646. .set_dr7 = vmx_set_dr7,
  10647. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  10648. .cache_reg = vmx_cache_reg,
  10649. .get_rflags = vmx_get_rflags,
  10650. .set_rflags = vmx_set_rflags,
  10651. .tlb_flush = vmx_flush_tlb,
  10652. .run = vmx_vcpu_run,
  10653. .handle_exit = vmx_handle_exit,
  10654. .skip_emulated_instruction = skip_emulated_instruction,
  10655. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  10656. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  10657. .patch_hypercall = vmx_patch_hypercall,
  10658. .set_irq = vmx_inject_irq,
  10659. .set_nmi = vmx_inject_nmi,
  10660. .queue_exception = vmx_queue_exception,
  10661. .cancel_injection = vmx_cancel_injection,
  10662. .interrupt_allowed = vmx_interrupt_allowed,
  10663. .nmi_allowed = vmx_nmi_allowed,
  10664. .get_nmi_mask = vmx_get_nmi_mask,
  10665. .set_nmi_mask = vmx_set_nmi_mask,
  10666. .enable_nmi_window = enable_nmi_window,
  10667. .enable_irq_window = enable_irq_window,
  10668. .update_cr8_intercept = update_cr8_intercept,
  10669. .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
  10670. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  10671. .get_enable_apicv = vmx_get_enable_apicv,
  10672. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  10673. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  10674. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  10675. .hwapic_irr_update = vmx_hwapic_irr_update,
  10676. .hwapic_isr_update = vmx_hwapic_isr_update,
  10677. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  10678. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  10679. .set_tss_addr = vmx_set_tss_addr,
  10680. .get_tdp_level = get_ept_level,
  10681. .get_mt_mask = vmx_get_mt_mask,
  10682. .get_exit_info = vmx_get_exit_info,
  10683. .get_lpage_level = vmx_get_lpage_level,
  10684. .cpuid_update = vmx_cpuid_update,
  10685. .rdtscp_supported = vmx_rdtscp_supported,
  10686. .invpcid_supported = vmx_invpcid_supported,
  10687. .set_supported_cpuid = vmx_set_supported_cpuid,
  10688. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  10689. .write_tsc_offset = vmx_write_tsc_offset,
  10690. .set_tdp_cr3 = vmx_set_cr3,
  10691. .check_intercept = vmx_check_intercept,
  10692. .handle_external_intr = vmx_handle_external_intr,
  10693. .mpx_supported = vmx_mpx_supported,
  10694. .xsaves_supported = vmx_xsaves_supported,
  10695. .umip_emulated = vmx_umip_emulated,
  10696. .check_nested_events = vmx_check_nested_events,
  10697. .sched_in = vmx_sched_in,
  10698. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  10699. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  10700. .flush_log_dirty = vmx_flush_log_dirty,
  10701. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  10702. .write_log_dirty = vmx_write_pml_buffer,
  10703. .pre_block = vmx_pre_block,
  10704. .post_block = vmx_post_block,
  10705. .pmu_ops = &intel_pmu_ops,
  10706. .update_pi_irte = vmx_update_pi_irte,
  10707. #ifdef CONFIG_X86_64
  10708. .set_hv_timer = vmx_set_hv_timer,
  10709. .cancel_hv_timer = vmx_cancel_hv_timer,
  10710. #endif
  10711. .setup_mce = vmx_setup_mce,
  10712. .smi_allowed = vmx_smi_allowed,
  10713. .pre_enter_smm = vmx_pre_enter_smm,
  10714. .pre_leave_smm = vmx_pre_leave_smm,
  10715. .enable_smi_window = enable_smi_window,
  10716. };
  10717. static int __init vmx_init(void)
  10718. {
  10719. int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  10720. __alignof__(struct vcpu_vmx), THIS_MODULE);
  10721. if (r)
  10722. return r;
  10723. #ifdef CONFIG_KEXEC_CORE
  10724. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  10725. crash_vmclear_local_loaded_vmcss);
  10726. #endif
  10727. return 0;
  10728. }
  10729. static void __exit vmx_exit(void)
  10730. {
  10731. #ifdef CONFIG_KEXEC_CORE
  10732. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  10733. synchronize_rcu();
  10734. #endif
  10735. kvm_exit();
  10736. }
  10737. module_init(vmx_init)
  10738. module_exit(vmx_exit)