vmx.c 329 KB

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