vmx.c 266 KB

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