vmx.c 294 KB

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