vmx.c 295 KB

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