vmx.c 249 KB

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