vmx.c 213 KB

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