x86.c 178 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * derived from drivers/kvm/kvm_main.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  10. *
  11. * Authors:
  12. * Avi Kivity <avi@qumranet.com>
  13. * Yaniv Kamay <yaniv@qumranet.com>
  14. * Amit Shah <amit.shah@qumranet.com>
  15. * Ben-Ami Yassour <benami@il.ibm.com>
  16. *
  17. * This work is licensed under the terms of the GNU GPL, version 2. See
  18. * the COPYING file in the top-level directory.
  19. *
  20. */
  21. #include <linux/kvm_host.h>
  22. #include "irq.h"
  23. #include "mmu.h"
  24. #include "i8254.h"
  25. #include "tss.h"
  26. #include "kvm_cache_regs.h"
  27. #include "x86.h"
  28. #include "cpuid.h"
  29. #include <linux/clocksource.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/kvm.h>
  32. #include <linux/fs.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/module.h>
  35. #include <linux/mman.h>
  36. #include <linux/highmem.h>
  37. #include <linux/iommu.h>
  38. #include <linux/intel-iommu.h>
  39. #include <linux/cpufreq.h>
  40. #include <linux/user-return-notifier.h>
  41. #include <linux/srcu.h>
  42. #include <linux/slab.h>
  43. #include <linux/perf_event.h>
  44. #include <linux/uaccess.h>
  45. #include <linux/hash.h>
  46. #include <linux/pci.h>
  47. #include <linux/timekeeper_internal.h>
  48. #include <linux/pvclock_gtod.h>
  49. #include <trace/events/kvm.h>
  50. #define CREATE_TRACE_POINTS
  51. #include "trace.h"
  52. #include <asm/debugreg.h>
  53. #include <asm/msr.h>
  54. #include <asm/desc.h>
  55. #include <asm/mtrr.h>
  56. #include <asm/mce.h>
  57. #include <asm/i387.h>
  58. #include <asm/fpu-internal.h> /* Ugh! */
  59. #include <asm/xcr.h>
  60. #include <asm/pvclock.h>
  61. #include <asm/div64.h>
  62. #define MAX_IO_MSRS 256
  63. #define KVM_MAX_MCE_BANKS 32
  64. #define KVM_MCE_CAP_SUPPORTED (MCG_CTL_P | MCG_SER_P)
  65. #define emul_to_vcpu(ctxt) \
  66. container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
  67. /* EFER defaults:
  68. * - enable syscall per default because its emulated by KVM
  69. * - enable LME and LMA per default on 64 bit KVM
  70. */
  71. #ifdef CONFIG_X86_64
  72. static
  73. u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
  74. #else
  75. static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
  76. #endif
  77. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  78. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  79. static void update_cr8_intercept(struct kvm_vcpu *vcpu);
  80. static void process_nmi(struct kvm_vcpu *vcpu);
  81. struct kvm_x86_ops *kvm_x86_ops;
  82. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  83. static bool ignore_msrs = 0;
  84. module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
  85. bool kvm_has_tsc_control;
  86. EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
  87. u32 kvm_max_guest_tsc_khz;
  88. EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
  89. /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
  90. static u32 tsc_tolerance_ppm = 250;
  91. module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
  92. #define KVM_NR_SHARED_MSRS 16
  93. struct kvm_shared_msrs_global {
  94. int nr;
  95. u32 msrs[KVM_NR_SHARED_MSRS];
  96. };
  97. struct kvm_shared_msrs {
  98. struct user_return_notifier urn;
  99. bool registered;
  100. struct kvm_shared_msr_values {
  101. u64 host;
  102. u64 curr;
  103. } values[KVM_NR_SHARED_MSRS];
  104. };
  105. static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
  106. static struct kvm_shared_msrs __percpu *shared_msrs;
  107. struct kvm_stats_debugfs_item debugfs_entries[] = {
  108. { "pf_fixed", VCPU_STAT(pf_fixed) },
  109. { "pf_guest", VCPU_STAT(pf_guest) },
  110. { "tlb_flush", VCPU_STAT(tlb_flush) },
  111. { "invlpg", VCPU_STAT(invlpg) },
  112. { "exits", VCPU_STAT(exits) },
  113. { "io_exits", VCPU_STAT(io_exits) },
  114. { "mmio_exits", VCPU_STAT(mmio_exits) },
  115. { "signal_exits", VCPU_STAT(signal_exits) },
  116. { "irq_window", VCPU_STAT(irq_window_exits) },
  117. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  118. { "halt_exits", VCPU_STAT(halt_exits) },
  119. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  120. { "hypercalls", VCPU_STAT(hypercalls) },
  121. { "request_irq", VCPU_STAT(request_irq_exits) },
  122. { "irq_exits", VCPU_STAT(irq_exits) },
  123. { "host_state_reload", VCPU_STAT(host_state_reload) },
  124. { "efer_reload", VCPU_STAT(efer_reload) },
  125. { "fpu_reload", VCPU_STAT(fpu_reload) },
  126. { "insn_emulation", VCPU_STAT(insn_emulation) },
  127. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  128. { "irq_injections", VCPU_STAT(irq_injections) },
  129. { "nmi_injections", VCPU_STAT(nmi_injections) },
  130. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  131. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  132. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  133. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  134. { "mmu_flooded", VM_STAT(mmu_flooded) },
  135. { "mmu_recycled", VM_STAT(mmu_recycled) },
  136. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  137. { "mmu_unsync", VM_STAT(mmu_unsync) },
  138. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  139. { "largepages", VM_STAT(lpages) },
  140. { NULL }
  141. };
  142. u64 __read_mostly host_xcr0;
  143. static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
  144. static int kvm_vcpu_reset(struct kvm_vcpu *vcpu);
  145. static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
  146. {
  147. int i;
  148. for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
  149. vcpu->arch.apf.gfns[i] = ~0;
  150. }
  151. static void kvm_on_user_return(struct user_return_notifier *urn)
  152. {
  153. unsigned slot;
  154. struct kvm_shared_msrs *locals
  155. = container_of(urn, struct kvm_shared_msrs, urn);
  156. struct kvm_shared_msr_values *values;
  157. for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
  158. values = &locals->values[slot];
  159. if (values->host != values->curr) {
  160. wrmsrl(shared_msrs_global.msrs[slot], values->host);
  161. values->curr = values->host;
  162. }
  163. }
  164. locals->registered = false;
  165. user_return_notifier_unregister(urn);
  166. }
  167. static void shared_msr_update(unsigned slot, u32 msr)
  168. {
  169. u64 value;
  170. unsigned int cpu = smp_processor_id();
  171. struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
  172. /* only read, and nobody should modify it at this time,
  173. * so don't need lock */
  174. if (slot >= shared_msrs_global.nr) {
  175. printk(KERN_ERR "kvm: invalid MSR slot!");
  176. return;
  177. }
  178. rdmsrl_safe(msr, &value);
  179. smsr->values[slot].host = value;
  180. smsr->values[slot].curr = value;
  181. }
  182. void kvm_define_shared_msr(unsigned slot, u32 msr)
  183. {
  184. if (slot >= shared_msrs_global.nr)
  185. shared_msrs_global.nr = slot + 1;
  186. shared_msrs_global.msrs[slot] = msr;
  187. /* we need ensured the shared_msr_global have been updated */
  188. smp_wmb();
  189. }
  190. EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
  191. static void kvm_shared_msr_cpu_online(void)
  192. {
  193. unsigned i;
  194. for (i = 0; i < shared_msrs_global.nr; ++i)
  195. shared_msr_update(i, shared_msrs_global.msrs[i]);
  196. }
  197. void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
  198. {
  199. unsigned int cpu = smp_processor_id();
  200. struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
  201. if (((value ^ smsr->values[slot].curr) & mask) == 0)
  202. return;
  203. smsr->values[slot].curr = value;
  204. wrmsrl(shared_msrs_global.msrs[slot], value);
  205. if (!smsr->registered) {
  206. smsr->urn.on_user_return = kvm_on_user_return;
  207. user_return_notifier_register(&smsr->urn);
  208. smsr->registered = true;
  209. }
  210. }
  211. EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
  212. static void drop_user_return_notifiers(void *ignore)
  213. {
  214. unsigned int cpu = smp_processor_id();
  215. struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
  216. if (smsr->registered)
  217. kvm_on_user_return(&smsr->urn);
  218. }
  219. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  220. {
  221. return vcpu->arch.apic_base;
  222. }
  223. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  224. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  225. {
  226. /* TODO: reserve bits check */
  227. kvm_lapic_set_base(vcpu, data);
  228. }
  229. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  230. #define EXCPT_BENIGN 0
  231. #define EXCPT_CONTRIBUTORY 1
  232. #define EXCPT_PF 2
  233. static int exception_class(int vector)
  234. {
  235. switch (vector) {
  236. case PF_VECTOR:
  237. return EXCPT_PF;
  238. case DE_VECTOR:
  239. case TS_VECTOR:
  240. case NP_VECTOR:
  241. case SS_VECTOR:
  242. case GP_VECTOR:
  243. return EXCPT_CONTRIBUTORY;
  244. default:
  245. break;
  246. }
  247. return EXCPT_BENIGN;
  248. }
  249. static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
  250. unsigned nr, bool has_error, u32 error_code,
  251. bool reinject)
  252. {
  253. u32 prev_nr;
  254. int class1, class2;
  255. kvm_make_request(KVM_REQ_EVENT, vcpu);
  256. if (!vcpu->arch.exception.pending) {
  257. queue:
  258. vcpu->arch.exception.pending = true;
  259. vcpu->arch.exception.has_error_code = has_error;
  260. vcpu->arch.exception.nr = nr;
  261. vcpu->arch.exception.error_code = error_code;
  262. vcpu->arch.exception.reinject = reinject;
  263. return;
  264. }
  265. /* to check exception */
  266. prev_nr = vcpu->arch.exception.nr;
  267. if (prev_nr == DF_VECTOR) {
  268. /* triple fault -> shutdown */
  269. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  270. return;
  271. }
  272. class1 = exception_class(prev_nr);
  273. class2 = exception_class(nr);
  274. if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
  275. || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
  276. /* generate double fault per SDM Table 5-5 */
  277. vcpu->arch.exception.pending = true;
  278. vcpu->arch.exception.has_error_code = true;
  279. vcpu->arch.exception.nr = DF_VECTOR;
  280. vcpu->arch.exception.error_code = 0;
  281. } else
  282. /* replace previous exception with a new one in a hope
  283. that instruction re-execution will regenerate lost
  284. exception */
  285. goto queue;
  286. }
  287. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  288. {
  289. kvm_multiple_exception(vcpu, nr, false, 0, false);
  290. }
  291. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  292. void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  293. {
  294. kvm_multiple_exception(vcpu, nr, false, 0, true);
  295. }
  296. EXPORT_SYMBOL_GPL(kvm_requeue_exception);
  297. void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
  298. {
  299. if (err)
  300. kvm_inject_gp(vcpu, 0);
  301. else
  302. kvm_x86_ops->skip_emulated_instruction(vcpu);
  303. }
  304. EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
  305. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
  306. {
  307. ++vcpu->stat.pf_guest;
  308. vcpu->arch.cr2 = fault->address;
  309. kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
  310. }
  311. EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
  312. void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
  313. {
  314. if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
  315. vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
  316. else
  317. vcpu->arch.mmu.inject_page_fault(vcpu, fault);
  318. }
  319. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  320. {
  321. atomic_inc(&vcpu->arch.nmi_queued);
  322. kvm_make_request(KVM_REQ_NMI, vcpu);
  323. }
  324. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  325. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  326. {
  327. kvm_multiple_exception(vcpu, nr, true, error_code, false);
  328. }
  329. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  330. void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  331. {
  332. kvm_multiple_exception(vcpu, nr, true, error_code, true);
  333. }
  334. EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
  335. /*
  336. * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
  337. * a #GP and return false.
  338. */
  339. bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
  340. {
  341. if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
  342. return true;
  343. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  344. return false;
  345. }
  346. EXPORT_SYMBOL_GPL(kvm_require_cpl);
  347. /*
  348. * This function will be used to read from the physical memory of the currently
  349. * running guest. The difference to kvm_read_guest_page is that this function
  350. * can read from guest physical or from the guest's guest physical memory.
  351. */
  352. int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  353. gfn_t ngfn, void *data, int offset, int len,
  354. u32 access)
  355. {
  356. gfn_t real_gfn;
  357. gpa_t ngpa;
  358. ngpa = gfn_to_gpa(ngfn);
  359. real_gfn = mmu->translate_gpa(vcpu, ngpa, access);
  360. if (real_gfn == UNMAPPED_GVA)
  361. return -EFAULT;
  362. real_gfn = gpa_to_gfn(real_gfn);
  363. return kvm_read_guest_page(vcpu->kvm, real_gfn, data, offset, len);
  364. }
  365. EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
  366. int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
  367. void *data, int offset, int len, u32 access)
  368. {
  369. return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
  370. data, offset, len, access);
  371. }
  372. /*
  373. * Load the pae pdptrs. Return true is they are all valid.
  374. */
  375. int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
  376. {
  377. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  378. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  379. int i;
  380. int ret;
  381. u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
  382. ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
  383. offset * sizeof(u64), sizeof(pdpte),
  384. PFERR_USER_MASK|PFERR_WRITE_MASK);
  385. if (ret < 0) {
  386. ret = 0;
  387. goto out;
  388. }
  389. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  390. if (is_present_gpte(pdpte[i]) &&
  391. (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
  392. ret = 0;
  393. goto out;
  394. }
  395. }
  396. ret = 1;
  397. memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
  398. __set_bit(VCPU_EXREG_PDPTR,
  399. (unsigned long *)&vcpu->arch.regs_avail);
  400. __set_bit(VCPU_EXREG_PDPTR,
  401. (unsigned long *)&vcpu->arch.regs_dirty);
  402. out:
  403. return ret;
  404. }
  405. EXPORT_SYMBOL_GPL(load_pdptrs);
  406. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  407. {
  408. u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
  409. bool changed = true;
  410. int offset;
  411. gfn_t gfn;
  412. int r;
  413. if (is_long_mode(vcpu) || !is_pae(vcpu))
  414. return false;
  415. if (!test_bit(VCPU_EXREG_PDPTR,
  416. (unsigned long *)&vcpu->arch.regs_avail))
  417. return true;
  418. gfn = (kvm_read_cr3(vcpu) & ~31u) >> PAGE_SHIFT;
  419. offset = (kvm_read_cr3(vcpu) & ~31u) & (PAGE_SIZE - 1);
  420. r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
  421. PFERR_USER_MASK | PFERR_WRITE_MASK);
  422. if (r < 0)
  423. goto out;
  424. changed = memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
  425. out:
  426. return changed;
  427. }
  428. int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  429. {
  430. unsigned long old_cr0 = kvm_read_cr0(vcpu);
  431. unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
  432. X86_CR0_CD | X86_CR0_NW;
  433. cr0 |= X86_CR0_ET;
  434. #ifdef CONFIG_X86_64
  435. if (cr0 & 0xffffffff00000000UL)
  436. return 1;
  437. #endif
  438. cr0 &= ~CR0_RESERVED_BITS;
  439. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
  440. return 1;
  441. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
  442. return 1;
  443. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  444. #ifdef CONFIG_X86_64
  445. if ((vcpu->arch.efer & EFER_LME)) {
  446. int cs_db, cs_l;
  447. if (!is_pae(vcpu))
  448. return 1;
  449. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  450. if (cs_l)
  451. return 1;
  452. } else
  453. #endif
  454. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
  455. kvm_read_cr3(vcpu)))
  456. return 1;
  457. }
  458. if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
  459. return 1;
  460. kvm_x86_ops->set_cr0(vcpu, cr0);
  461. if ((cr0 ^ old_cr0) & X86_CR0_PG) {
  462. kvm_clear_async_pf_completion_queue(vcpu);
  463. kvm_async_pf_hash_reset(vcpu);
  464. }
  465. if ((cr0 ^ old_cr0) & update_bits)
  466. kvm_mmu_reset_context(vcpu);
  467. return 0;
  468. }
  469. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  470. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  471. {
  472. (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
  473. }
  474. EXPORT_SYMBOL_GPL(kvm_lmsw);
  475. int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
  476. {
  477. u64 xcr0;
  478. /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
  479. if (index != XCR_XFEATURE_ENABLED_MASK)
  480. return 1;
  481. xcr0 = xcr;
  482. if (kvm_x86_ops->get_cpl(vcpu) != 0)
  483. return 1;
  484. if (!(xcr0 & XSTATE_FP))
  485. return 1;
  486. if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
  487. return 1;
  488. if (xcr0 & ~host_xcr0)
  489. return 1;
  490. vcpu->arch.xcr0 = xcr0;
  491. vcpu->guest_xcr0_loaded = 0;
  492. return 0;
  493. }
  494. int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
  495. {
  496. if (__kvm_set_xcr(vcpu, index, xcr)) {
  497. kvm_inject_gp(vcpu, 0);
  498. return 1;
  499. }
  500. return 0;
  501. }
  502. EXPORT_SYMBOL_GPL(kvm_set_xcr);
  503. int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  504. {
  505. unsigned long old_cr4 = kvm_read_cr4(vcpu);
  506. unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE |
  507. X86_CR4_PAE | X86_CR4_SMEP;
  508. if (cr4 & CR4_RESERVED_BITS)
  509. return 1;
  510. if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
  511. return 1;
  512. if (!guest_cpuid_has_smep(vcpu) && (cr4 & X86_CR4_SMEP))
  513. return 1;
  514. if (!guest_cpuid_has_fsgsbase(vcpu) && (cr4 & X86_CR4_RDWRGSFS))
  515. return 1;
  516. if (is_long_mode(vcpu)) {
  517. if (!(cr4 & X86_CR4_PAE))
  518. return 1;
  519. } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
  520. && ((cr4 ^ old_cr4) & pdptr_bits)
  521. && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
  522. kvm_read_cr3(vcpu)))
  523. return 1;
  524. if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
  525. if (!guest_cpuid_has_pcid(vcpu))
  526. return 1;
  527. /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
  528. if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
  529. return 1;
  530. }
  531. if (kvm_x86_ops->set_cr4(vcpu, cr4))
  532. return 1;
  533. if (((cr4 ^ old_cr4) & pdptr_bits) ||
  534. (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
  535. kvm_mmu_reset_context(vcpu);
  536. if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
  537. kvm_update_cpuid(vcpu);
  538. return 0;
  539. }
  540. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  541. int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  542. {
  543. if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
  544. kvm_mmu_sync_roots(vcpu);
  545. kvm_mmu_flush_tlb(vcpu);
  546. return 0;
  547. }
  548. if (is_long_mode(vcpu)) {
  549. if (kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)) {
  550. if (cr3 & CR3_PCID_ENABLED_RESERVED_BITS)
  551. return 1;
  552. } else
  553. if (cr3 & CR3_L_MODE_RESERVED_BITS)
  554. return 1;
  555. } else {
  556. if (is_pae(vcpu)) {
  557. if (cr3 & CR3_PAE_RESERVED_BITS)
  558. return 1;
  559. if (is_paging(vcpu) &&
  560. !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
  561. return 1;
  562. }
  563. /*
  564. * We don't check reserved bits in nonpae mode, because
  565. * this isn't enforced, and VMware depends on this.
  566. */
  567. }
  568. /*
  569. * Does the new cr3 value map to physical memory? (Note, we
  570. * catch an invalid cr3 even in real-mode, because it would
  571. * cause trouble later on when we turn on paging anyway.)
  572. *
  573. * A real CPU would silently accept an invalid cr3 and would
  574. * attempt to use it - with largely undefined (and often hard
  575. * to debug) behavior on the guest side.
  576. */
  577. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  578. return 1;
  579. vcpu->arch.cr3 = cr3;
  580. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  581. vcpu->arch.mmu.new_cr3(vcpu);
  582. return 0;
  583. }
  584. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  585. int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  586. {
  587. if (cr8 & CR8_RESERVED_BITS)
  588. return 1;
  589. if (irqchip_in_kernel(vcpu->kvm))
  590. kvm_lapic_set_tpr(vcpu, cr8);
  591. else
  592. vcpu->arch.cr8 = cr8;
  593. return 0;
  594. }
  595. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  596. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  597. {
  598. if (irqchip_in_kernel(vcpu->kvm))
  599. return kvm_lapic_get_cr8(vcpu);
  600. else
  601. return vcpu->arch.cr8;
  602. }
  603. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  604. static void kvm_update_dr7(struct kvm_vcpu *vcpu)
  605. {
  606. unsigned long dr7;
  607. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  608. dr7 = vcpu->arch.guest_debug_dr7;
  609. else
  610. dr7 = vcpu->arch.dr7;
  611. kvm_x86_ops->set_dr7(vcpu, dr7);
  612. vcpu->arch.switch_db_regs = (dr7 & DR7_BP_EN_MASK);
  613. }
  614. static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
  615. {
  616. switch (dr) {
  617. case 0 ... 3:
  618. vcpu->arch.db[dr] = val;
  619. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  620. vcpu->arch.eff_db[dr] = val;
  621. break;
  622. case 4:
  623. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  624. return 1; /* #UD */
  625. /* fall through */
  626. case 6:
  627. if (val & 0xffffffff00000000ULL)
  628. return -1; /* #GP */
  629. vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
  630. break;
  631. case 5:
  632. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  633. return 1; /* #UD */
  634. /* fall through */
  635. default: /* 7 */
  636. if (val & 0xffffffff00000000ULL)
  637. return -1; /* #GP */
  638. vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
  639. kvm_update_dr7(vcpu);
  640. break;
  641. }
  642. return 0;
  643. }
  644. int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
  645. {
  646. int res;
  647. res = __kvm_set_dr(vcpu, dr, val);
  648. if (res > 0)
  649. kvm_queue_exception(vcpu, UD_VECTOR);
  650. else if (res < 0)
  651. kvm_inject_gp(vcpu, 0);
  652. return res;
  653. }
  654. EXPORT_SYMBOL_GPL(kvm_set_dr);
  655. static int _kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
  656. {
  657. switch (dr) {
  658. case 0 ... 3:
  659. *val = vcpu->arch.db[dr];
  660. break;
  661. case 4:
  662. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  663. return 1;
  664. /* fall through */
  665. case 6:
  666. *val = vcpu->arch.dr6;
  667. break;
  668. case 5:
  669. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  670. return 1;
  671. /* fall through */
  672. default: /* 7 */
  673. *val = vcpu->arch.dr7;
  674. break;
  675. }
  676. return 0;
  677. }
  678. int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
  679. {
  680. if (_kvm_get_dr(vcpu, dr, val)) {
  681. kvm_queue_exception(vcpu, UD_VECTOR);
  682. return 1;
  683. }
  684. return 0;
  685. }
  686. EXPORT_SYMBOL_GPL(kvm_get_dr);
  687. bool kvm_rdpmc(struct kvm_vcpu *vcpu)
  688. {
  689. u32 ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  690. u64 data;
  691. int err;
  692. err = kvm_pmu_read_pmc(vcpu, ecx, &data);
  693. if (err)
  694. return err;
  695. kvm_register_write(vcpu, VCPU_REGS_RAX, (u32)data);
  696. kvm_register_write(vcpu, VCPU_REGS_RDX, data >> 32);
  697. return err;
  698. }
  699. EXPORT_SYMBOL_GPL(kvm_rdpmc);
  700. /*
  701. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  702. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  703. *
  704. * This list is modified at module load time to reflect the
  705. * capabilities of the host cpu. This capabilities test skips MSRs that are
  706. * kvm-specific. Those are put in the beginning of the list.
  707. */
  708. #define KVM_SAVE_MSRS_BEGIN 10
  709. static u32 msrs_to_save[] = {
  710. MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  711. MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
  712. HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
  713. HV_X64_MSR_APIC_ASSIST_PAGE, MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
  714. MSR_KVM_PV_EOI_EN,
  715. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  716. MSR_STAR,
  717. #ifdef CONFIG_X86_64
  718. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  719. #endif
  720. MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
  721. };
  722. static unsigned num_msrs_to_save;
  723. static const u32 emulated_msrs[] = {
  724. MSR_IA32_TSC_ADJUST,
  725. MSR_IA32_TSCDEADLINE,
  726. MSR_IA32_MISC_ENABLE,
  727. MSR_IA32_MCG_STATUS,
  728. MSR_IA32_MCG_CTL,
  729. };
  730. static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
  731. {
  732. u64 old_efer = vcpu->arch.efer;
  733. if (efer & efer_reserved_bits)
  734. return 1;
  735. if (is_paging(vcpu)
  736. && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
  737. return 1;
  738. if (efer & EFER_FFXSR) {
  739. struct kvm_cpuid_entry2 *feat;
  740. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  741. if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
  742. return 1;
  743. }
  744. if (efer & EFER_SVME) {
  745. struct kvm_cpuid_entry2 *feat;
  746. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  747. if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
  748. return 1;
  749. }
  750. efer &= ~EFER_LMA;
  751. efer |= vcpu->arch.efer & EFER_LMA;
  752. kvm_x86_ops->set_efer(vcpu, efer);
  753. vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
  754. /* Update reserved bits */
  755. if ((efer ^ old_efer) & EFER_NX)
  756. kvm_mmu_reset_context(vcpu);
  757. return 0;
  758. }
  759. void kvm_enable_efer_bits(u64 mask)
  760. {
  761. efer_reserved_bits &= ~mask;
  762. }
  763. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  764. /*
  765. * Writes msr value into into the appropriate "register".
  766. * Returns 0 on success, non-0 otherwise.
  767. * Assumes vcpu_load() was already called.
  768. */
  769. int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  770. {
  771. return kvm_x86_ops->set_msr(vcpu, msr);
  772. }
  773. /*
  774. * Adapt set_msr() to msr_io()'s calling convention
  775. */
  776. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  777. {
  778. struct msr_data msr;
  779. msr.data = *data;
  780. msr.index = index;
  781. msr.host_initiated = true;
  782. return kvm_set_msr(vcpu, &msr);
  783. }
  784. #ifdef CONFIG_X86_64
  785. struct pvclock_gtod_data {
  786. seqcount_t seq;
  787. struct { /* extract of a clocksource struct */
  788. int vclock_mode;
  789. cycle_t cycle_last;
  790. cycle_t mask;
  791. u32 mult;
  792. u32 shift;
  793. } clock;
  794. /* open coded 'struct timespec' */
  795. u64 monotonic_time_snsec;
  796. time_t monotonic_time_sec;
  797. };
  798. static struct pvclock_gtod_data pvclock_gtod_data;
  799. static void update_pvclock_gtod(struct timekeeper *tk)
  800. {
  801. struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
  802. write_seqcount_begin(&vdata->seq);
  803. /* copy pvclock gtod data */
  804. vdata->clock.vclock_mode = tk->clock->archdata.vclock_mode;
  805. vdata->clock.cycle_last = tk->clock->cycle_last;
  806. vdata->clock.mask = tk->clock->mask;
  807. vdata->clock.mult = tk->mult;
  808. vdata->clock.shift = tk->shift;
  809. vdata->monotonic_time_sec = tk->xtime_sec
  810. + tk->wall_to_monotonic.tv_sec;
  811. vdata->monotonic_time_snsec = tk->xtime_nsec
  812. + (tk->wall_to_monotonic.tv_nsec
  813. << tk->shift);
  814. while (vdata->monotonic_time_snsec >=
  815. (((u64)NSEC_PER_SEC) << tk->shift)) {
  816. vdata->monotonic_time_snsec -=
  817. ((u64)NSEC_PER_SEC) << tk->shift;
  818. vdata->monotonic_time_sec++;
  819. }
  820. write_seqcount_end(&vdata->seq);
  821. }
  822. #endif
  823. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  824. {
  825. int version;
  826. int r;
  827. struct pvclock_wall_clock wc;
  828. struct timespec boot;
  829. if (!wall_clock)
  830. return;
  831. r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
  832. if (r)
  833. return;
  834. if (version & 1)
  835. ++version; /* first time write, random junk */
  836. ++version;
  837. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  838. /*
  839. * The guest calculates current wall clock time by adding
  840. * system time (updated by kvm_guest_time_update below) to the
  841. * wall clock specified here. guest system time equals host
  842. * system time for us, thus we must fill in host boot time here.
  843. */
  844. getboottime(&boot);
  845. if (kvm->arch.kvmclock_offset) {
  846. struct timespec ts = ns_to_timespec(kvm->arch.kvmclock_offset);
  847. boot = timespec_sub(boot, ts);
  848. }
  849. wc.sec = boot.tv_sec;
  850. wc.nsec = boot.tv_nsec;
  851. wc.version = version;
  852. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  853. version++;
  854. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  855. }
  856. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  857. {
  858. uint32_t quotient, remainder;
  859. /* Don't try to replace with do_div(), this one calculates
  860. * "(dividend << 32) / divisor" */
  861. __asm__ ( "divl %4"
  862. : "=a" (quotient), "=d" (remainder)
  863. : "0" (0), "1" (dividend), "r" (divisor) );
  864. return quotient;
  865. }
  866. static void kvm_get_time_scale(uint32_t scaled_khz, uint32_t base_khz,
  867. s8 *pshift, u32 *pmultiplier)
  868. {
  869. uint64_t scaled64;
  870. int32_t shift = 0;
  871. uint64_t tps64;
  872. uint32_t tps32;
  873. tps64 = base_khz * 1000LL;
  874. scaled64 = scaled_khz * 1000LL;
  875. while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
  876. tps64 >>= 1;
  877. shift--;
  878. }
  879. tps32 = (uint32_t)tps64;
  880. while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
  881. if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
  882. scaled64 >>= 1;
  883. else
  884. tps32 <<= 1;
  885. shift++;
  886. }
  887. *pshift = shift;
  888. *pmultiplier = div_frac(scaled64, tps32);
  889. pr_debug("%s: base_khz %u => %u, shift %d, mul %u\n",
  890. __func__, base_khz, scaled_khz, shift, *pmultiplier);
  891. }
  892. static inline u64 get_kernel_ns(void)
  893. {
  894. struct timespec ts;
  895. WARN_ON(preemptible());
  896. ktime_get_ts(&ts);
  897. monotonic_to_bootbased(&ts);
  898. return timespec_to_ns(&ts);
  899. }
  900. #ifdef CONFIG_X86_64
  901. static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
  902. #endif
  903. static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
  904. unsigned long max_tsc_khz;
  905. static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
  906. {
  907. return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
  908. vcpu->arch.virtual_tsc_shift);
  909. }
  910. static u32 adjust_tsc_khz(u32 khz, s32 ppm)
  911. {
  912. u64 v = (u64)khz * (1000000 + ppm);
  913. do_div(v, 1000000);
  914. return v;
  915. }
  916. static void kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 this_tsc_khz)
  917. {
  918. u32 thresh_lo, thresh_hi;
  919. int use_scaling = 0;
  920. /* Compute a scale to convert nanoseconds in TSC cycles */
  921. kvm_get_time_scale(this_tsc_khz, NSEC_PER_SEC / 1000,
  922. &vcpu->arch.virtual_tsc_shift,
  923. &vcpu->arch.virtual_tsc_mult);
  924. vcpu->arch.virtual_tsc_khz = this_tsc_khz;
  925. /*
  926. * Compute the variation in TSC rate which is acceptable
  927. * within the range of tolerance and decide if the
  928. * rate being applied is within that bounds of the hardware
  929. * rate. If so, no scaling or compensation need be done.
  930. */
  931. thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
  932. thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
  933. if (this_tsc_khz < thresh_lo || this_tsc_khz > thresh_hi) {
  934. pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", this_tsc_khz, thresh_lo, thresh_hi);
  935. use_scaling = 1;
  936. }
  937. kvm_x86_ops->set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
  938. }
  939. static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
  940. {
  941. u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
  942. vcpu->arch.virtual_tsc_mult,
  943. vcpu->arch.virtual_tsc_shift);
  944. tsc += vcpu->arch.this_tsc_write;
  945. return tsc;
  946. }
  947. void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
  948. {
  949. #ifdef CONFIG_X86_64
  950. bool vcpus_matched;
  951. bool do_request = false;
  952. struct kvm_arch *ka = &vcpu->kvm->arch;
  953. struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
  954. vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
  955. atomic_read(&vcpu->kvm->online_vcpus));
  956. if (vcpus_matched && gtod->clock.vclock_mode == VCLOCK_TSC)
  957. if (!ka->use_master_clock)
  958. do_request = 1;
  959. if (!vcpus_matched && ka->use_master_clock)
  960. do_request = 1;
  961. if (do_request)
  962. kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
  963. trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
  964. atomic_read(&vcpu->kvm->online_vcpus),
  965. ka->use_master_clock, gtod->clock.vclock_mode);
  966. #endif
  967. }
  968. static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
  969. {
  970. u64 curr_offset = kvm_x86_ops->read_tsc_offset(vcpu);
  971. vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
  972. }
  973. void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
  974. {
  975. struct kvm *kvm = vcpu->kvm;
  976. u64 offset, ns, elapsed;
  977. unsigned long flags;
  978. s64 usdiff;
  979. bool matched;
  980. u64 data = msr->data;
  981. raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
  982. offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
  983. ns = get_kernel_ns();
  984. elapsed = ns - kvm->arch.last_tsc_nsec;
  985. /* n.b - signed multiplication and division required */
  986. usdiff = data - kvm->arch.last_tsc_write;
  987. #ifdef CONFIG_X86_64
  988. usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
  989. #else
  990. /* do_div() only does unsigned */
  991. asm("idivl %2; xor %%edx, %%edx"
  992. : "=A"(usdiff)
  993. : "A"(usdiff * 1000), "rm"(vcpu->arch.virtual_tsc_khz));
  994. #endif
  995. do_div(elapsed, 1000);
  996. usdiff -= elapsed;
  997. if (usdiff < 0)
  998. usdiff = -usdiff;
  999. /*
  1000. * Special case: TSC write with a small delta (1 second) of virtual
  1001. * cycle time against real time is interpreted as an attempt to
  1002. * synchronize the CPU.
  1003. *
  1004. * For a reliable TSC, we can match TSC offsets, and for an unstable
  1005. * TSC, we add elapsed time in this computation. We could let the
  1006. * compensation code attempt to catch up if we fall behind, but
  1007. * it's better to try to match offsets from the beginning.
  1008. */
  1009. if (usdiff < USEC_PER_SEC &&
  1010. vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
  1011. if (!check_tsc_unstable()) {
  1012. offset = kvm->arch.cur_tsc_offset;
  1013. pr_debug("kvm: matched tsc offset for %llu\n", data);
  1014. } else {
  1015. u64 delta = nsec_to_cycles(vcpu, elapsed);
  1016. data += delta;
  1017. offset = kvm_x86_ops->compute_tsc_offset(vcpu, data);
  1018. pr_debug("kvm: adjusted tsc offset by %llu\n", delta);
  1019. }
  1020. matched = true;
  1021. } else {
  1022. /*
  1023. * We split periods of matched TSC writes into generations.
  1024. * For each generation, we track the original measured
  1025. * nanosecond time, offset, and write, so if TSCs are in
  1026. * sync, we can match exact offset, and if not, we can match
  1027. * exact software computation in compute_guest_tsc()
  1028. *
  1029. * These values are tracked in kvm->arch.cur_xxx variables.
  1030. */
  1031. kvm->arch.cur_tsc_generation++;
  1032. kvm->arch.cur_tsc_nsec = ns;
  1033. kvm->arch.cur_tsc_write = data;
  1034. kvm->arch.cur_tsc_offset = offset;
  1035. matched = false;
  1036. pr_debug("kvm: new tsc generation %u, clock %llu\n",
  1037. kvm->arch.cur_tsc_generation, data);
  1038. }
  1039. /*
  1040. * We also track th most recent recorded KHZ, write and time to
  1041. * allow the matching interval to be extended at each write.
  1042. */
  1043. kvm->arch.last_tsc_nsec = ns;
  1044. kvm->arch.last_tsc_write = data;
  1045. kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
  1046. /* Reset of TSC must disable overshoot protection below */
  1047. vcpu->arch.hv_clock.tsc_timestamp = 0;
  1048. vcpu->arch.last_guest_tsc = data;
  1049. /* Keep track of which generation this VCPU has synchronized to */
  1050. vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
  1051. vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
  1052. vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
  1053. if (guest_cpuid_has_tsc_adjust(vcpu) && !msr->host_initiated)
  1054. update_ia32_tsc_adjust_msr(vcpu, offset);
  1055. kvm_x86_ops->write_tsc_offset(vcpu, offset);
  1056. raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
  1057. spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
  1058. if (matched)
  1059. kvm->arch.nr_vcpus_matched_tsc++;
  1060. else
  1061. kvm->arch.nr_vcpus_matched_tsc = 0;
  1062. kvm_track_tsc_matching(vcpu);
  1063. spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
  1064. }
  1065. EXPORT_SYMBOL_GPL(kvm_write_tsc);
  1066. #ifdef CONFIG_X86_64
  1067. static cycle_t read_tsc(void)
  1068. {
  1069. cycle_t ret;
  1070. u64 last;
  1071. /*
  1072. * Empirically, a fence (of type that depends on the CPU)
  1073. * before rdtsc is enough to ensure that rdtsc is ordered
  1074. * with respect to loads. The various CPU manuals are unclear
  1075. * as to whether rdtsc can be reordered with later loads,
  1076. * but no one has ever seen it happen.
  1077. */
  1078. rdtsc_barrier();
  1079. ret = (cycle_t)vget_cycles();
  1080. last = pvclock_gtod_data.clock.cycle_last;
  1081. if (likely(ret >= last))
  1082. return ret;
  1083. /*
  1084. * GCC likes to generate cmov here, but this branch is extremely
  1085. * predictable (it's just a funciton of time and the likely is
  1086. * very likely) and there's a data dependence, so force GCC
  1087. * to generate a branch instead. I don't barrier() because
  1088. * we don't actually need a barrier, and if this function
  1089. * ever gets inlined it will generate worse code.
  1090. */
  1091. asm volatile ("");
  1092. return last;
  1093. }
  1094. static inline u64 vgettsc(cycle_t *cycle_now)
  1095. {
  1096. long v;
  1097. struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
  1098. *cycle_now = read_tsc();
  1099. v = (*cycle_now - gtod->clock.cycle_last) & gtod->clock.mask;
  1100. return v * gtod->clock.mult;
  1101. }
  1102. static int do_monotonic(struct timespec *ts, cycle_t *cycle_now)
  1103. {
  1104. unsigned long seq;
  1105. u64 ns;
  1106. int mode;
  1107. struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
  1108. ts->tv_nsec = 0;
  1109. do {
  1110. seq = read_seqcount_begin(&gtod->seq);
  1111. mode = gtod->clock.vclock_mode;
  1112. ts->tv_sec = gtod->monotonic_time_sec;
  1113. ns = gtod->monotonic_time_snsec;
  1114. ns += vgettsc(cycle_now);
  1115. ns >>= gtod->clock.shift;
  1116. } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
  1117. timespec_add_ns(ts, ns);
  1118. return mode;
  1119. }
  1120. /* returns true if host is using tsc clocksource */
  1121. static bool kvm_get_time_and_clockread(s64 *kernel_ns, cycle_t *cycle_now)
  1122. {
  1123. struct timespec ts;
  1124. /* checked again under seqlock below */
  1125. if (pvclock_gtod_data.clock.vclock_mode != VCLOCK_TSC)
  1126. return false;
  1127. if (do_monotonic(&ts, cycle_now) != VCLOCK_TSC)
  1128. return false;
  1129. monotonic_to_bootbased(&ts);
  1130. *kernel_ns = timespec_to_ns(&ts);
  1131. return true;
  1132. }
  1133. #endif
  1134. /*
  1135. *
  1136. * Assuming a stable TSC across physical CPUS, and a stable TSC
  1137. * across virtual CPUs, the following condition is possible.
  1138. * Each numbered line represents an event visible to both
  1139. * CPUs at the next numbered event.
  1140. *
  1141. * "timespecX" represents host monotonic time. "tscX" represents
  1142. * RDTSC value.
  1143. *
  1144. * VCPU0 on CPU0 | VCPU1 on CPU1
  1145. *
  1146. * 1. read timespec0,tsc0
  1147. * 2. | timespec1 = timespec0 + N
  1148. * | tsc1 = tsc0 + M
  1149. * 3. transition to guest | transition to guest
  1150. * 4. ret0 = timespec0 + (rdtsc - tsc0) |
  1151. * 5. | ret1 = timespec1 + (rdtsc - tsc1)
  1152. * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
  1153. *
  1154. * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
  1155. *
  1156. * - ret0 < ret1
  1157. * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
  1158. * ...
  1159. * - 0 < N - M => M < N
  1160. *
  1161. * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
  1162. * always the case (the difference between two distinct xtime instances
  1163. * might be smaller then the difference between corresponding TSC reads,
  1164. * when updating guest vcpus pvclock areas).
  1165. *
  1166. * To avoid that problem, do not allow visibility of distinct
  1167. * system_timestamp/tsc_timestamp values simultaneously: use a master
  1168. * copy of host monotonic time values. Update that master copy
  1169. * in lockstep.
  1170. *
  1171. * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
  1172. *
  1173. */
  1174. static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
  1175. {
  1176. #ifdef CONFIG_X86_64
  1177. struct kvm_arch *ka = &kvm->arch;
  1178. int vclock_mode;
  1179. bool host_tsc_clocksource, vcpus_matched;
  1180. vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
  1181. atomic_read(&kvm->online_vcpus));
  1182. /*
  1183. * If the host uses TSC clock, then passthrough TSC as stable
  1184. * to the guest.
  1185. */
  1186. host_tsc_clocksource = kvm_get_time_and_clockread(
  1187. &ka->master_kernel_ns,
  1188. &ka->master_cycle_now);
  1189. ka->use_master_clock = host_tsc_clocksource & vcpus_matched;
  1190. if (ka->use_master_clock)
  1191. atomic_set(&kvm_guest_has_master_clock, 1);
  1192. vclock_mode = pvclock_gtod_data.clock.vclock_mode;
  1193. trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
  1194. vcpus_matched);
  1195. #endif
  1196. }
  1197. static int kvm_guest_time_update(struct kvm_vcpu *v)
  1198. {
  1199. unsigned long flags, this_tsc_khz;
  1200. struct kvm_vcpu_arch *vcpu = &v->arch;
  1201. struct kvm_arch *ka = &v->kvm->arch;
  1202. void *shared_kaddr;
  1203. s64 kernel_ns, max_kernel_ns;
  1204. u64 tsc_timestamp, host_tsc;
  1205. struct pvclock_vcpu_time_info *guest_hv_clock;
  1206. u8 pvclock_flags;
  1207. bool use_master_clock;
  1208. kernel_ns = 0;
  1209. host_tsc = 0;
  1210. /* Keep irq disabled to prevent changes to the clock */
  1211. local_irq_save(flags);
  1212. this_tsc_khz = __get_cpu_var(cpu_tsc_khz);
  1213. if (unlikely(this_tsc_khz == 0)) {
  1214. local_irq_restore(flags);
  1215. kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
  1216. return 1;
  1217. }
  1218. /*
  1219. * If the host uses TSC clock, then passthrough TSC as stable
  1220. * to the guest.
  1221. */
  1222. spin_lock(&ka->pvclock_gtod_sync_lock);
  1223. use_master_clock = ka->use_master_clock;
  1224. if (use_master_clock) {
  1225. host_tsc = ka->master_cycle_now;
  1226. kernel_ns = ka->master_kernel_ns;
  1227. }
  1228. spin_unlock(&ka->pvclock_gtod_sync_lock);
  1229. if (!use_master_clock) {
  1230. host_tsc = native_read_tsc();
  1231. kernel_ns = get_kernel_ns();
  1232. }
  1233. tsc_timestamp = kvm_x86_ops->read_l1_tsc(v, host_tsc);
  1234. /*
  1235. * We may have to catch up the TSC to match elapsed wall clock
  1236. * time for two reasons, even if kvmclock is used.
  1237. * 1) CPU could have been running below the maximum TSC rate
  1238. * 2) Broken TSC compensation resets the base at each VCPU
  1239. * entry to avoid unknown leaps of TSC even when running
  1240. * again on the same CPU. This may cause apparent elapsed
  1241. * time to disappear, and the guest to stand still or run
  1242. * very slowly.
  1243. */
  1244. if (vcpu->tsc_catchup) {
  1245. u64 tsc = compute_guest_tsc(v, kernel_ns);
  1246. if (tsc > tsc_timestamp) {
  1247. adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
  1248. tsc_timestamp = tsc;
  1249. }
  1250. }
  1251. local_irq_restore(flags);
  1252. if (!vcpu->time_page)
  1253. return 0;
  1254. /*
  1255. * Time as measured by the TSC may go backwards when resetting the base
  1256. * tsc_timestamp. The reason for this is that the TSC resolution is
  1257. * higher than the resolution of the other clock scales. Thus, many
  1258. * possible measurments of the TSC correspond to one measurement of any
  1259. * other clock, and so a spread of values is possible. This is not a
  1260. * problem for the computation of the nanosecond clock; with TSC rates
  1261. * around 1GHZ, there can only be a few cycles which correspond to one
  1262. * nanosecond value, and any path through this code will inevitably
  1263. * take longer than that. However, with the kernel_ns value itself,
  1264. * the precision may be much lower, down to HZ granularity. If the
  1265. * first sampling of TSC against kernel_ns ends in the low part of the
  1266. * range, and the second in the high end of the range, we can get:
  1267. *
  1268. * (TSC - offset_low) * S + kns_old > (TSC - offset_high) * S + kns_new
  1269. *
  1270. * As the sampling errors potentially range in the thousands of cycles,
  1271. * it is possible such a time value has already been observed by the
  1272. * guest. To protect against this, we must compute the system time as
  1273. * observed by the guest and ensure the new system time is greater.
  1274. */
  1275. max_kernel_ns = 0;
  1276. if (vcpu->hv_clock.tsc_timestamp) {
  1277. max_kernel_ns = vcpu->last_guest_tsc -
  1278. vcpu->hv_clock.tsc_timestamp;
  1279. max_kernel_ns = pvclock_scale_delta(max_kernel_ns,
  1280. vcpu->hv_clock.tsc_to_system_mul,
  1281. vcpu->hv_clock.tsc_shift);
  1282. max_kernel_ns += vcpu->last_kernel_ns;
  1283. }
  1284. if (unlikely(vcpu->hw_tsc_khz != this_tsc_khz)) {
  1285. kvm_get_time_scale(NSEC_PER_SEC / 1000, this_tsc_khz,
  1286. &vcpu->hv_clock.tsc_shift,
  1287. &vcpu->hv_clock.tsc_to_system_mul);
  1288. vcpu->hw_tsc_khz = this_tsc_khz;
  1289. }
  1290. /* with a master <monotonic time, tsc value> tuple,
  1291. * pvclock clock reads always increase at the (scaled) rate
  1292. * of guest TSC - no need to deal with sampling errors.
  1293. */
  1294. if (!use_master_clock) {
  1295. if (max_kernel_ns > kernel_ns)
  1296. kernel_ns = max_kernel_ns;
  1297. }
  1298. /* With all the info we got, fill in the values */
  1299. vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
  1300. vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
  1301. vcpu->last_kernel_ns = kernel_ns;
  1302. vcpu->last_guest_tsc = tsc_timestamp;
  1303. /*
  1304. * The interface expects us to write an even number signaling that the
  1305. * update is finished. Since the guest won't see the intermediate
  1306. * state, we just increase by 2 at the end.
  1307. */
  1308. vcpu->hv_clock.version += 2;
  1309. shared_kaddr = kmap_atomic(vcpu->time_page);
  1310. guest_hv_clock = shared_kaddr + vcpu->time_offset;
  1311. /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
  1312. pvclock_flags = (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED);
  1313. if (vcpu->pvclock_set_guest_stopped_request) {
  1314. pvclock_flags |= PVCLOCK_GUEST_STOPPED;
  1315. vcpu->pvclock_set_guest_stopped_request = false;
  1316. }
  1317. /* If the host uses TSC clocksource, then it is stable */
  1318. if (use_master_clock)
  1319. pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
  1320. vcpu->hv_clock.flags = pvclock_flags;
  1321. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  1322. sizeof(vcpu->hv_clock));
  1323. kunmap_atomic(shared_kaddr);
  1324. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  1325. return 0;
  1326. }
  1327. static bool msr_mtrr_valid(unsigned msr)
  1328. {
  1329. switch (msr) {
  1330. case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
  1331. case MSR_MTRRfix64K_00000:
  1332. case MSR_MTRRfix16K_80000:
  1333. case MSR_MTRRfix16K_A0000:
  1334. case MSR_MTRRfix4K_C0000:
  1335. case MSR_MTRRfix4K_C8000:
  1336. case MSR_MTRRfix4K_D0000:
  1337. case MSR_MTRRfix4K_D8000:
  1338. case MSR_MTRRfix4K_E0000:
  1339. case MSR_MTRRfix4K_E8000:
  1340. case MSR_MTRRfix4K_F0000:
  1341. case MSR_MTRRfix4K_F8000:
  1342. case MSR_MTRRdefType:
  1343. case MSR_IA32_CR_PAT:
  1344. return true;
  1345. case 0x2f8:
  1346. return true;
  1347. }
  1348. return false;
  1349. }
  1350. static bool valid_pat_type(unsigned t)
  1351. {
  1352. return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
  1353. }
  1354. static bool valid_mtrr_type(unsigned t)
  1355. {
  1356. return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
  1357. }
  1358. static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1359. {
  1360. int i;
  1361. if (!msr_mtrr_valid(msr))
  1362. return false;
  1363. if (msr == MSR_IA32_CR_PAT) {
  1364. for (i = 0; i < 8; i++)
  1365. if (!valid_pat_type((data >> (i * 8)) & 0xff))
  1366. return false;
  1367. return true;
  1368. } else if (msr == MSR_MTRRdefType) {
  1369. if (data & ~0xcff)
  1370. return false;
  1371. return valid_mtrr_type(data & 0xff);
  1372. } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
  1373. for (i = 0; i < 8 ; i++)
  1374. if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
  1375. return false;
  1376. return true;
  1377. }
  1378. /* variable MTRRs */
  1379. return valid_mtrr_type(data & 0xff);
  1380. }
  1381. static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1382. {
  1383. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  1384. if (!mtrr_valid(vcpu, msr, data))
  1385. return 1;
  1386. if (msr == MSR_MTRRdefType) {
  1387. vcpu->arch.mtrr_state.def_type = data;
  1388. vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
  1389. } else if (msr == MSR_MTRRfix64K_00000)
  1390. p[0] = data;
  1391. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  1392. p[1 + msr - MSR_MTRRfix16K_80000] = data;
  1393. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  1394. p[3 + msr - MSR_MTRRfix4K_C0000] = data;
  1395. else if (msr == MSR_IA32_CR_PAT)
  1396. vcpu->arch.pat = data;
  1397. else { /* Variable MTRRs */
  1398. int idx, is_mtrr_mask;
  1399. u64 *pt;
  1400. idx = (msr - 0x200) / 2;
  1401. is_mtrr_mask = msr - 0x200 - 2 * idx;
  1402. if (!is_mtrr_mask)
  1403. pt =
  1404. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  1405. else
  1406. pt =
  1407. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  1408. *pt = data;
  1409. }
  1410. kvm_mmu_reset_context(vcpu);
  1411. return 0;
  1412. }
  1413. static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1414. {
  1415. u64 mcg_cap = vcpu->arch.mcg_cap;
  1416. unsigned bank_num = mcg_cap & 0xff;
  1417. switch (msr) {
  1418. case MSR_IA32_MCG_STATUS:
  1419. vcpu->arch.mcg_status = data;
  1420. break;
  1421. case MSR_IA32_MCG_CTL:
  1422. if (!(mcg_cap & MCG_CTL_P))
  1423. return 1;
  1424. if (data != 0 && data != ~(u64)0)
  1425. return -1;
  1426. vcpu->arch.mcg_ctl = data;
  1427. break;
  1428. default:
  1429. if (msr >= MSR_IA32_MC0_CTL &&
  1430. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  1431. u32 offset = msr - MSR_IA32_MC0_CTL;
  1432. /* only 0 or all 1s can be written to IA32_MCi_CTL
  1433. * some Linux kernels though clear bit 10 in bank 4 to
  1434. * workaround a BIOS/GART TBL issue on AMD K8s, ignore
  1435. * this to avoid an uncatched #GP in the guest
  1436. */
  1437. if ((offset & 0x3) == 0 &&
  1438. data != 0 && (data | (1 << 10)) != ~(u64)0)
  1439. return -1;
  1440. vcpu->arch.mce_banks[offset] = data;
  1441. break;
  1442. }
  1443. return 1;
  1444. }
  1445. return 0;
  1446. }
  1447. static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
  1448. {
  1449. struct kvm *kvm = vcpu->kvm;
  1450. int lm = is_long_mode(vcpu);
  1451. u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
  1452. : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
  1453. u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
  1454. : kvm->arch.xen_hvm_config.blob_size_32;
  1455. u32 page_num = data & ~PAGE_MASK;
  1456. u64 page_addr = data & PAGE_MASK;
  1457. u8 *page;
  1458. int r;
  1459. r = -E2BIG;
  1460. if (page_num >= blob_size)
  1461. goto out;
  1462. r = -ENOMEM;
  1463. page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
  1464. if (IS_ERR(page)) {
  1465. r = PTR_ERR(page);
  1466. goto out;
  1467. }
  1468. if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
  1469. goto out_free;
  1470. r = 0;
  1471. out_free:
  1472. kfree(page);
  1473. out:
  1474. return r;
  1475. }
  1476. static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
  1477. {
  1478. return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
  1479. }
  1480. static bool kvm_hv_msr_partition_wide(u32 msr)
  1481. {
  1482. bool r = false;
  1483. switch (msr) {
  1484. case HV_X64_MSR_GUEST_OS_ID:
  1485. case HV_X64_MSR_HYPERCALL:
  1486. r = true;
  1487. break;
  1488. }
  1489. return r;
  1490. }
  1491. static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1492. {
  1493. struct kvm *kvm = vcpu->kvm;
  1494. switch (msr) {
  1495. case HV_X64_MSR_GUEST_OS_ID:
  1496. kvm->arch.hv_guest_os_id = data;
  1497. /* setting guest os id to zero disables hypercall page */
  1498. if (!kvm->arch.hv_guest_os_id)
  1499. kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
  1500. break;
  1501. case HV_X64_MSR_HYPERCALL: {
  1502. u64 gfn;
  1503. unsigned long addr;
  1504. u8 instructions[4];
  1505. /* if guest os id is not set hypercall should remain disabled */
  1506. if (!kvm->arch.hv_guest_os_id)
  1507. break;
  1508. if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
  1509. kvm->arch.hv_hypercall = data;
  1510. break;
  1511. }
  1512. gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
  1513. addr = gfn_to_hva(kvm, gfn);
  1514. if (kvm_is_error_hva(addr))
  1515. return 1;
  1516. kvm_x86_ops->patch_hypercall(vcpu, instructions);
  1517. ((unsigned char *)instructions)[3] = 0xc3; /* ret */
  1518. if (__copy_to_user((void __user *)addr, instructions, 4))
  1519. return 1;
  1520. kvm->arch.hv_hypercall = data;
  1521. break;
  1522. }
  1523. default:
  1524. vcpu_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
  1525. "data 0x%llx\n", msr, data);
  1526. return 1;
  1527. }
  1528. return 0;
  1529. }
  1530. static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1531. {
  1532. switch (msr) {
  1533. case HV_X64_MSR_APIC_ASSIST_PAGE: {
  1534. unsigned long addr;
  1535. if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
  1536. vcpu->arch.hv_vapic = data;
  1537. break;
  1538. }
  1539. addr = gfn_to_hva(vcpu->kvm, data >>
  1540. HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
  1541. if (kvm_is_error_hva(addr))
  1542. return 1;
  1543. if (__clear_user((void __user *)addr, PAGE_SIZE))
  1544. return 1;
  1545. vcpu->arch.hv_vapic = data;
  1546. break;
  1547. }
  1548. case HV_X64_MSR_EOI:
  1549. return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
  1550. case HV_X64_MSR_ICR:
  1551. return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
  1552. case HV_X64_MSR_TPR:
  1553. return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
  1554. default:
  1555. vcpu_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
  1556. "data 0x%llx\n", msr, data);
  1557. return 1;
  1558. }
  1559. return 0;
  1560. }
  1561. static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
  1562. {
  1563. gpa_t gpa = data & ~0x3f;
  1564. /* Bits 2:5 are reserved, Should be zero */
  1565. if (data & 0x3c)
  1566. return 1;
  1567. vcpu->arch.apf.msr_val = data;
  1568. if (!(data & KVM_ASYNC_PF_ENABLED)) {
  1569. kvm_clear_async_pf_completion_queue(vcpu);
  1570. kvm_async_pf_hash_reset(vcpu);
  1571. return 0;
  1572. }
  1573. if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa))
  1574. return 1;
  1575. vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
  1576. kvm_async_pf_wakeup_all(vcpu);
  1577. return 0;
  1578. }
  1579. static void kvmclock_reset(struct kvm_vcpu *vcpu)
  1580. {
  1581. if (vcpu->arch.time_page) {
  1582. kvm_release_page_dirty(vcpu->arch.time_page);
  1583. vcpu->arch.time_page = NULL;
  1584. }
  1585. }
  1586. static void accumulate_steal_time(struct kvm_vcpu *vcpu)
  1587. {
  1588. u64 delta;
  1589. if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
  1590. return;
  1591. delta = current->sched_info.run_delay - vcpu->arch.st.last_steal;
  1592. vcpu->arch.st.last_steal = current->sched_info.run_delay;
  1593. vcpu->arch.st.accum_steal = delta;
  1594. }
  1595. static void record_steal_time(struct kvm_vcpu *vcpu)
  1596. {
  1597. if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
  1598. return;
  1599. if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
  1600. &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
  1601. return;
  1602. vcpu->arch.st.steal.steal += vcpu->arch.st.accum_steal;
  1603. vcpu->arch.st.steal.version += 2;
  1604. vcpu->arch.st.accum_steal = 0;
  1605. kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
  1606. &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
  1607. }
  1608. int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  1609. {
  1610. bool pr = false;
  1611. u32 msr = msr_info->index;
  1612. u64 data = msr_info->data;
  1613. switch (msr) {
  1614. case MSR_AMD64_NB_CFG:
  1615. case MSR_IA32_UCODE_REV:
  1616. case MSR_IA32_UCODE_WRITE:
  1617. case MSR_VM_HSAVE_PA:
  1618. case MSR_AMD64_PATCH_LOADER:
  1619. case MSR_AMD64_BU_CFG2:
  1620. break;
  1621. case MSR_EFER:
  1622. return set_efer(vcpu, data);
  1623. case MSR_K7_HWCR:
  1624. data &= ~(u64)0x40; /* ignore flush filter disable */
  1625. data &= ~(u64)0x100; /* ignore ignne emulation enable */
  1626. data &= ~(u64)0x8; /* ignore TLB cache disable */
  1627. if (data != 0) {
  1628. vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
  1629. data);
  1630. return 1;
  1631. }
  1632. break;
  1633. case MSR_FAM10H_MMIO_CONF_BASE:
  1634. if (data != 0) {
  1635. vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
  1636. "0x%llx\n", data);
  1637. return 1;
  1638. }
  1639. break;
  1640. case MSR_IA32_DEBUGCTLMSR:
  1641. if (!data) {
  1642. /* We support the non-activated case already */
  1643. break;
  1644. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  1645. /* Values other than LBR and BTF are vendor-specific,
  1646. thus reserved and should throw a #GP */
  1647. return 1;
  1648. }
  1649. vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  1650. __func__, data);
  1651. break;
  1652. case 0x200 ... 0x2ff:
  1653. return set_msr_mtrr(vcpu, msr, data);
  1654. case MSR_IA32_APICBASE:
  1655. kvm_set_apic_base(vcpu, data);
  1656. break;
  1657. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1658. return kvm_x2apic_msr_write(vcpu, msr, data);
  1659. case MSR_IA32_TSCDEADLINE:
  1660. kvm_set_lapic_tscdeadline_msr(vcpu, data);
  1661. break;
  1662. case MSR_IA32_TSC_ADJUST:
  1663. if (guest_cpuid_has_tsc_adjust(vcpu)) {
  1664. if (!msr_info->host_initiated) {
  1665. u64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
  1666. kvm_x86_ops->adjust_tsc_offset(vcpu, adj, true);
  1667. }
  1668. vcpu->arch.ia32_tsc_adjust_msr = data;
  1669. }
  1670. break;
  1671. case MSR_IA32_MISC_ENABLE:
  1672. vcpu->arch.ia32_misc_enable_msr = data;
  1673. break;
  1674. case MSR_KVM_WALL_CLOCK_NEW:
  1675. case MSR_KVM_WALL_CLOCK:
  1676. vcpu->kvm->arch.wall_clock = data;
  1677. kvm_write_wall_clock(vcpu->kvm, data);
  1678. break;
  1679. case MSR_KVM_SYSTEM_TIME_NEW:
  1680. case MSR_KVM_SYSTEM_TIME: {
  1681. kvmclock_reset(vcpu);
  1682. vcpu->arch.time = data;
  1683. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  1684. /* we verify if the enable bit is set... */
  1685. if (!(data & 1))
  1686. break;
  1687. /* ...but clean it before doing the actual write */
  1688. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  1689. vcpu->arch.time_page =
  1690. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  1691. if (is_error_page(vcpu->arch.time_page))
  1692. vcpu->arch.time_page = NULL;
  1693. break;
  1694. }
  1695. case MSR_KVM_ASYNC_PF_EN:
  1696. if (kvm_pv_enable_async_pf(vcpu, data))
  1697. return 1;
  1698. break;
  1699. case MSR_KVM_STEAL_TIME:
  1700. if (unlikely(!sched_info_on()))
  1701. return 1;
  1702. if (data & KVM_STEAL_RESERVED_MASK)
  1703. return 1;
  1704. if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
  1705. data & KVM_STEAL_VALID_BITS))
  1706. return 1;
  1707. vcpu->arch.st.msr_val = data;
  1708. if (!(data & KVM_MSR_ENABLED))
  1709. break;
  1710. vcpu->arch.st.last_steal = current->sched_info.run_delay;
  1711. preempt_disable();
  1712. accumulate_steal_time(vcpu);
  1713. preempt_enable();
  1714. kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
  1715. break;
  1716. case MSR_KVM_PV_EOI_EN:
  1717. if (kvm_lapic_enable_pv_eoi(vcpu, data))
  1718. return 1;
  1719. break;
  1720. case MSR_IA32_MCG_CTL:
  1721. case MSR_IA32_MCG_STATUS:
  1722. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1723. return set_msr_mce(vcpu, msr, data);
  1724. /* Performance counters are not protected by a CPUID bit,
  1725. * so we should check all of them in the generic path for the sake of
  1726. * cross vendor migration.
  1727. * Writing a zero into the event select MSRs disables them,
  1728. * which we perfectly emulate ;-). Any other value should be at least
  1729. * reported, some guests depend on them.
  1730. */
  1731. case MSR_K7_EVNTSEL0:
  1732. case MSR_K7_EVNTSEL1:
  1733. case MSR_K7_EVNTSEL2:
  1734. case MSR_K7_EVNTSEL3:
  1735. if (data != 0)
  1736. vcpu_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1737. "0x%x data 0x%llx\n", msr, data);
  1738. break;
  1739. /* at least RHEL 4 unconditionally writes to the perfctr registers,
  1740. * so we ignore writes to make it happy.
  1741. */
  1742. case MSR_K7_PERFCTR0:
  1743. case MSR_K7_PERFCTR1:
  1744. case MSR_K7_PERFCTR2:
  1745. case MSR_K7_PERFCTR3:
  1746. vcpu_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1747. "0x%x data 0x%llx\n", msr, data);
  1748. break;
  1749. case MSR_P6_PERFCTR0:
  1750. case MSR_P6_PERFCTR1:
  1751. pr = true;
  1752. case MSR_P6_EVNTSEL0:
  1753. case MSR_P6_EVNTSEL1:
  1754. if (kvm_pmu_msr(vcpu, msr))
  1755. return kvm_pmu_set_msr(vcpu, msr, data);
  1756. if (pr || data != 0)
  1757. vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
  1758. "0x%x data 0x%llx\n", msr, data);
  1759. break;
  1760. case MSR_K7_CLK_CTL:
  1761. /*
  1762. * Ignore all writes to this no longer documented MSR.
  1763. * Writes are only relevant for old K7 processors,
  1764. * all pre-dating SVM, but a recommended workaround from
  1765. * AMD for these chips. It is possible to specify the
  1766. * affected processor models on the command line, hence
  1767. * the need to ignore the workaround.
  1768. */
  1769. break;
  1770. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1771. if (kvm_hv_msr_partition_wide(msr)) {
  1772. int r;
  1773. mutex_lock(&vcpu->kvm->lock);
  1774. r = set_msr_hyperv_pw(vcpu, msr, data);
  1775. mutex_unlock(&vcpu->kvm->lock);
  1776. return r;
  1777. } else
  1778. return set_msr_hyperv(vcpu, msr, data);
  1779. break;
  1780. case MSR_IA32_BBL_CR_CTL3:
  1781. /* Drop writes to this legacy MSR -- see rdmsr
  1782. * counterpart for further detail.
  1783. */
  1784. vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
  1785. break;
  1786. case MSR_AMD64_OSVW_ID_LENGTH:
  1787. if (!guest_cpuid_has_osvw(vcpu))
  1788. return 1;
  1789. vcpu->arch.osvw.length = data;
  1790. break;
  1791. case MSR_AMD64_OSVW_STATUS:
  1792. if (!guest_cpuid_has_osvw(vcpu))
  1793. return 1;
  1794. vcpu->arch.osvw.status = data;
  1795. break;
  1796. default:
  1797. if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
  1798. return xen_hvm_config(vcpu, data);
  1799. if (kvm_pmu_msr(vcpu, msr))
  1800. return kvm_pmu_set_msr(vcpu, msr, data);
  1801. if (!ignore_msrs) {
  1802. vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
  1803. msr, data);
  1804. return 1;
  1805. } else {
  1806. vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
  1807. msr, data);
  1808. break;
  1809. }
  1810. }
  1811. return 0;
  1812. }
  1813. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1814. /*
  1815. * Reads an msr value (of 'msr_index') into 'pdata'.
  1816. * Returns 0 on success, non-0 otherwise.
  1817. * Assumes vcpu_load() was already called.
  1818. */
  1819. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1820. {
  1821. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  1822. }
  1823. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1824. {
  1825. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  1826. if (!msr_mtrr_valid(msr))
  1827. return 1;
  1828. if (msr == MSR_MTRRdefType)
  1829. *pdata = vcpu->arch.mtrr_state.def_type +
  1830. (vcpu->arch.mtrr_state.enabled << 10);
  1831. else if (msr == MSR_MTRRfix64K_00000)
  1832. *pdata = p[0];
  1833. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  1834. *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
  1835. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  1836. *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
  1837. else if (msr == MSR_IA32_CR_PAT)
  1838. *pdata = vcpu->arch.pat;
  1839. else { /* Variable MTRRs */
  1840. int idx, is_mtrr_mask;
  1841. u64 *pt;
  1842. idx = (msr - 0x200) / 2;
  1843. is_mtrr_mask = msr - 0x200 - 2 * idx;
  1844. if (!is_mtrr_mask)
  1845. pt =
  1846. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  1847. else
  1848. pt =
  1849. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  1850. *pdata = *pt;
  1851. }
  1852. return 0;
  1853. }
  1854. static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1855. {
  1856. u64 data;
  1857. u64 mcg_cap = vcpu->arch.mcg_cap;
  1858. unsigned bank_num = mcg_cap & 0xff;
  1859. switch (msr) {
  1860. case MSR_IA32_P5_MC_ADDR:
  1861. case MSR_IA32_P5_MC_TYPE:
  1862. data = 0;
  1863. break;
  1864. case MSR_IA32_MCG_CAP:
  1865. data = vcpu->arch.mcg_cap;
  1866. break;
  1867. case MSR_IA32_MCG_CTL:
  1868. if (!(mcg_cap & MCG_CTL_P))
  1869. return 1;
  1870. data = vcpu->arch.mcg_ctl;
  1871. break;
  1872. case MSR_IA32_MCG_STATUS:
  1873. data = vcpu->arch.mcg_status;
  1874. break;
  1875. default:
  1876. if (msr >= MSR_IA32_MC0_CTL &&
  1877. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  1878. u32 offset = msr - MSR_IA32_MC0_CTL;
  1879. data = vcpu->arch.mce_banks[offset];
  1880. break;
  1881. }
  1882. return 1;
  1883. }
  1884. *pdata = data;
  1885. return 0;
  1886. }
  1887. static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1888. {
  1889. u64 data = 0;
  1890. struct kvm *kvm = vcpu->kvm;
  1891. switch (msr) {
  1892. case HV_X64_MSR_GUEST_OS_ID:
  1893. data = kvm->arch.hv_guest_os_id;
  1894. break;
  1895. case HV_X64_MSR_HYPERCALL:
  1896. data = kvm->arch.hv_hypercall;
  1897. break;
  1898. default:
  1899. vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1900. return 1;
  1901. }
  1902. *pdata = data;
  1903. return 0;
  1904. }
  1905. static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1906. {
  1907. u64 data = 0;
  1908. switch (msr) {
  1909. case HV_X64_MSR_VP_INDEX: {
  1910. int r;
  1911. struct kvm_vcpu *v;
  1912. kvm_for_each_vcpu(r, v, vcpu->kvm)
  1913. if (v == vcpu)
  1914. data = r;
  1915. break;
  1916. }
  1917. case HV_X64_MSR_EOI:
  1918. return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
  1919. case HV_X64_MSR_ICR:
  1920. return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
  1921. case HV_X64_MSR_TPR:
  1922. return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
  1923. case HV_X64_MSR_APIC_ASSIST_PAGE:
  1924. data = vcpu->arch.hv_vapic;
  1925. break;
  1926. default:
  1927. vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1928. return 1;
  1929. }
  1930. *pdata = data;
  1931. return 0;
  1932. }
  1933. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1934. {
  1935. u64 data;
  1936. switch (msr) {
  1937. case MSR_IA32_PLATFORM_ID:
  1938. case MSR_IA32_EBL_CR_POWERON:
  1939. case MSR_IA32_DEBUGCTLMSR:
  1940. case MSR_IA32_LASTBRANCHFROMIP:
  1941. case MSR_IA32_LASTBRANCHTOIP:
  1942. case MSR_IA32_LASTINTFROMIP:
  1943. case MSR_IA32_LASTINTTOIP:
  1944. case MSR_K8_SYSCFG:
  1945. case MSR_K7_HWCR:
  1946. case MSR_VM_HSAVE_PA:
  1947. case MSR_K7_EVNTSEL0:
  1948. case MSR_K7_PERFCTR0:
  1949. case MSR_K8_INT_PENDING_MSG:
  1950. case MSR_AMD64_NB_CFG:
  1951. case MSR_FAM10H_MMIO_CONF_BASE:
  1952. case MSR_AMD64_BU_CFG2:
  1953. data = 0;
  1954. break;
  1955. case MSR_P6_PERFCTR0:
  1956. case MSR_P6_PERFCTR1:
  1957. case MSR_P6_EVNTSEL0:
  1958. case MSR_P6_EVNTSEL1:
  1959. if (kvm_pmu_msr(vcpu, msr))
  1960. return kvm_pmu_get_msr(vcpu, msr, pdata);
  1961. data = 0;
  1962. break;
  1963. case MSR_IA32_UCODE_REV:
  1964. data = 0x100000000ULL;
  1965. break;
  1966. case MSR_MTRRcap:
  1967. data = 0x500 | KVM_NR_VAR_MTRR;
  1968. break;
  1969. case 0x200 ... 0x2ff:
  1970. return get_msr_mtrr(vcpu, msr, pdata);
  1971. case 0xcd: /* fsb frequency */
  1972. data = 3;
  1973. break;
  1974. /*
  1975. * MSR_EBC_FREQUENCY_ID
  1976. * Conservative value valid for even the basic CPU models.
  1977. * Models 0,1: 000 in bits 23:21 indicating a bus speed of
  1978. * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
  1979. * and 266MHz for model 3, or 4. Set Core Clock
  1980. * Frequency to System Bus Frequency Ratio to 1 (bits
  1981. * 31:24) even though these are only valid for CPU
  1982. * models > 2, however guests may end up dividing or
  1983. * multiplying by zero otherwise.
  1984. */
  1985. case MSR_EBC_FREQUENCY_ID:
  1986. data = 1 << 24;
  1987. break;
  1988. case MSR_IA32_APICBASE:
  1989. data = kvm_get_apic_base(vcpu);
  1990. break;
  1991. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1992. return kvm_x2apic_msr_read(vcpu, msr, pdata);
  1993. break;
  1994. case MSR_IA32_TSCDEADLINE:
  1995. data = kvm_get_lapic_tscdeadline_msr(vcpu);
  1996. break;
  1997. case MSR_IA32_TSC_ADJUST:
  1998. data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
  1999. break;
  2000. case MSR_IA32_MISC_ENABLE:
  2001. data = vcpu->arch.ia32_misc_enable_msr;
  2002. break;
  2003. case MSR_IA32_PERF_STATUS:
  2004. /* TSC increment by tick */
  2005. data = 1000ULL;
  2006. /* CPU multiplier */
  2007. data |= (((uint64_t)4ULL) << 40);
  2008. break;
  2009. case MSR_EFER:
  2010. data = vcpu->arch.efer;
  2011. break;
  2012. case MSR_KVM_WALL_CLOCK:
  2013. case MSR_KVM_WALL_CLOCK_NEW:
  2014. data = vcpu->kvm->arch.wall_clock;
  2015. break;
  2016. case MSR_KVM_SYSTEM_TIME:
  2017. case MSR_KVM_SYSTEM_TIME_NEW:
  2018. data = vcpu->arch.time;
  2019. break;
  2020. case MSR_KVM_ASYNC_PF_EN:
  2021. data = vcpu->arch.apf.msr_val;
  2022. break;
  2023. case MSR_KVM_STEAL_TIME:
  2024. data = vcpu->arch.st.msr_val;
  2025. break;
  2026. case MSR_KVM_PV_EOI_EN:
  2027. data = vcpu->arch.pv_eoi.msr_val;
  2028. break;
  2029. case MSR_IA32_P5_MC_ADDR:
  2030. case MSR_IA32_P5_MC_TYPE:
  2031. case MSR_IA32_MCG_CAP:
  2032. case MSR_IA32_MCG_CTL:
  2033. case MSR_IA32_MCG_STATUS:
  2034. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  2035. return get_msr_mce(vcpu, msr, pdata);
  2036. case MSR_K7_CLK_CTL:
  2037. /*
  2038. * Provide expected ramp-up count for K7. All other
  2039. * are set to zero, indicating minimum divisors for
  2040. * every field.
  2041. *
  2042. * This prevents guest kernels on AMD host with CPU
  2043. * type 6, model 8 and higher from exploding due to
  2044. * the rdmsr failing.
  2045. */
  2046. data = 0x20000000;
  2047. break;
  2048. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  2049. if (kvm_hv_msr_partition_wide(msr)) {
  2050. int r;
  2051. mutex_lock(&vcpu->kvm->lock);
  2052. r = get_msr_hyperv_pw(vcpu, msr, pdata);
  2053. mutex_unlock(&vcpu->kvm->lock);
  2054. return r;
  2055. } else
  2056. return get_msr_hyperv(vcpu, msr, pdata);
  2057. break;
  2058. case MSR_IA32_BBL_CR_CTL3:
  2059. /* This legacy MSR exists but isn't fully documented in current
  2060. * silicon. It is however accessed by winxp in very narrow
  2061. * scenarios where it sets bit #19, itself documented as
  2062. * a "reserved" bit. Best effort attempt to source coherent
  2063. * read data here should the balance of the register be
  2064. * interpreted by the guest:
  2065. *
  2066. * L2 cache control register 3: 64GB range, 256KB size,
  2067. * enabled, latency 0x1, configured
  2068. */
  2069. data = 0xbe702111;
  2070. break;
  2071. case MSR_AMD64_OSVW_ID_LENGTH:
  2072. if (!guest_cpuid_has_osvw(vcpu))
  2073. return 1;
  2074. data = vcpu->arch.osvw.length;
  2075. break;
  2076. case MSR_AMD64_OSVW_STATUS:
  2077. if (!guest_cpuid_has_osvw(vcpu))
  2078. return 1;
  2079. data = vcpu->arch.osvw.status;
  2080. break;
  2081. default:
  2082. if (kvm_pmu_msr(vcpu, msr))
  2083. return kvm_pmu_get_msr(vcpu, msr, pdata);
  2084. if (!ignore_msrs) {
  2085. vcpu_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  2086. return 1;
  2087. } else {
  2088. vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
  2089. data = 0;
  2090. }
  2091. break;
  2092. }
  2093. *pdata = data;
  2094. return 0;
  2095. }
  2096. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  2097. /*
  2098. * Read or write a bunch of msrs. All parameters are kernel addresses.
  2099. *
  2100. * @return number of msrs set successfully.
  2101. */
  2102. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  2103. struct kvm_msr_entry *entries,
  2104. int (*do_msr)(struct kvm_vcpu *vcpu,
  2105. unsigned index, u64 *data))
  2106. {
  2107. int i, idx;
  2108. idx = srcu_read_lock(&vcpu->kvm->srcu);
  2109. for (i = 0; i < msrs->nmsrs; ++i)
  2110. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  2111. break;
  2112. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  2113. return i;
  2114. }
  2115. /*
  2116. * Read or write a bunch of msrs. Parameters are user addresses.
  2117. *
  2118. * @return number of msrs set successfully.
  2119. */
  2120. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  2121. int (*do_msr)(struct kvm_vcpu *vcpu,
  2122. unsigned index, u64 *data),
  2123. int writeback)
  2124. {
  2125. struct kvm_msrs msrs;
  2126. struct kvm_msr_entry *entries;
  2127. int r, n;
  2128. unsigned size;
  2129. r = -EFAULT;
  2130. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  2131. goto out;
  2132. r = -E2BIG;
  2133. if (msrs.nmsrs >= MAX_IO_MSRS)
  2134. goto out;
  2135. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  2136. entries = memdup_user(user_msrs->entries, size);
  2137. if (IS_ERR(entries)) {
  2138. r = PTR_ERR(entries);
  2139. goto out;
  2140. }
  2141. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  2142. if (r < 0)
  2143. goto out_free;
  2144. r = -EFAULT;
  2145. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  2146. goto out_free;
  2147. r = n;
  2148. out_free:
  2149. kfree(entries);
  2150. out:
  2151. return r;
  2152. }
  2153. int kvm_dev_ioctl_check_extension(long ext)
  2154. {
  2155. int r;
  2156. switch (ext) {
  2157. case KVM_CAP_IRQCHIP:
  2158. case KVM_CAP_HLT:
  2159. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  2160. case KVM_CAP_SET_TSS_ADDR:
  2161. case KVM_CAP_EXT_CPUID:
  2162. case KVM_CAP_CLOCKSOURCE:
  2163. case KVM_CAP_PIT:
  2164. case KVM_CAP_NOP_IO_DELAY:
  2165. case KVM_CAP_MP_STATE:
  2166. case KVM_CAP_SYNC_MMU:
  2167. case KVM_CAP_USER_NMI:
  2168. case KVM_CAP_REINJECT_CONTROL:
  2169. case KVM_CAP_IRQ_INJECT_STATUS:
  2170. case KVM_CAP_ASSIGN_DEV_IRQ:
  2171. case KVM_CAP_IRQFD:
  2172. case KVM_CAP_IOEVENTFD:
  2173. case KVM_CAP_PIT2:
  2174. case KVM_CAP_PIT_STATE2:
  2175. case KVM_CAP_SET_IDENTITY_MAP_ADDR:
  2176. case KVM_CAP_XEN_HVM:
  2177. case KVM_CAP_ADJUST_CLOCK:
  2178. case KVM_CAP_VCPU_EVENTS:
  2179. case KVM_CAP_HYPERV:
  2180. case KVM_CAP_HYPERV_VAPIC:
  2181. case KVM_CAP_HYPERV_SPIN:
  2182. case KVM_CAP_PCI_SEGMENT:
  2183. case KVM_CAP_DEBUGREGS:
  2184. case KVM_CAP_X86_ROBUST_SINGLESTEP:
  2185. case KVM_CAP_XSAVE:
  2186. case KVM_CAP_ASYNC_PF:
  2187. case KVM_CAP_GET_TSC_KHZ:
  2188. case KVM_CAP_PCI_2_3:
  2189. case KVM_CAP_KVMCLOCK_CTRL:
  2190. case KVM_CAP_READONLY_MEM:
  2191. case KVM_CAP_IRQFD_RESAMPLE:
  2192. r = 1;
  2193. break;
  2194. case KVM_CAP_COALESCED_MMIO:
  2195. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  2196. break;
  2197. case KVM_CAP_VAPIC:
  2198. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  2199. break;
  2200. case KVM_CAP_NR_VCPUS:
  2201. r = KVM_SOFT_MAX_VCPUS;
  2202. break;
  2203. case KVM_CAP_MAX_VCPUS:
  2204. r = KVM_MAX_VCPUS;
  2205. break;
  2206. case KVM_CAP_NR_MEMSLOTS:
  2207. r = KVM_MEMORY_SLOTS;
  2208. break;
  2209. case KVM_CAP_PV_MMU: /* obsolete */
  2210. r = 0;
  2211. break;
  2212. case KVM_CAP_IOMMU:
  2213. r = iommu_present(&pci_bus_type);
  2214. break;
  2215. case KVM_CAP_MCE:
  2216. r = KVM_MAX_MCE_BANKS;
  2217. break;
  2218. case KVM_CAP_XCRS:
  2219. r = cpu_has_xsave;
  2220. break;
  2221. case KVM_CAP_TSC_CONTROL:
  2222. r = kvm_has_tsc_control;
  2223. break;
  2224. case KVM_CAP_TSC_DEADLINE_TIMER:
  2225. r = boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER);
  2226. break;
  2227. default:
  2228. r = 0;
  2229. break;
  2230. }
  2231. return r;
  2232. }
  2233. long kvm_arch_dev_ioctl(struct file *filp,
  2234. unsigned int ioctl, unsigned long arg)
  2235. {
  2236. void __user *argp = (void __user *)arg;
  2237. long r;
  2238. switch (ioctl) {
  2239. case KVM_GET_MSR_INDEX_LIST: {
  2240. struct kvm_msr_list __user *user_msr_list = argp;
  2241. struct kvm_msr_list msr_list;
  2242. unsigned n;
  2243. r = -EFAULT;
  2244. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2245. goto out;
  2246. n = msr_list.nmsrs;
  2247. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2248. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2249. goto out;
  2250. r = -E2BIG;
  2251. if (n < msr_list.nmsrs)
  2252. goto out;
  2253. r = -EFAULT;
  2254. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2255. num_msrs_to_save * sizeof(u32)))
  2256. goto out;
  2257. if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
  2258. &emulated_msrs,
  2259. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2260. goto out;
  2261. r = 0;
  2262. break;
  2263. }
  2264. case KVM_GET_SUPPORTED_CPUID: {
  2265. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2266. struct kvm_cpuid2 cpuid;
  2267. r = -EFAULT;
  2268. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2269. goto out;
  2270. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  2271. cpuid_arg->entries);
  2272. if (r)
  2273. goto out;
  2274. r = -EFAULT;
  2275. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  2276. goto out;
  2277. r = 0;
  2278. break;
  2279. }
  2280. case KVM_X86_GET_MCE_CAP_SUPPORTED: {
  2281. u64 mce_cap;
  2282. mce_cap = KVM_MCE_CAP_SUPPORTED;
  2283. r = -EFAULT;
  2284. if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
  2285. goto out;
  2286. r = 0;
  2287. break;
  2288. }
  2289. default:
  2290. r = -EINVAL;
  2291. }
  2292. out:
  2293. return r;
  2294. }
  2295. static void wbinvd_ipi(void *garbage)
  2296. {
  2297. wbinvd();
  2298. }
  2299. static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
  2300. {
  2301. return vcpu->kvm->arch.iommu_domain &&
  2302. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY);
  2303. }
  2304. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2305. {
  2306. /* Address WBINVD may be executed by guest */
  2307. if (need_emulate_wbinvd(vcpu)) {
  2308. if (kvm_x86_ops->has_wbinvd_exit())
  2309. cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
  2310. else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
  2311. smp_call_function_single(vcpu->cpu,
  2312. wbinvd_ipi, NULL, 1);
  2313. }
  2314. kvm_x86_ops->vcpu_load(vcpu, cpu);
  2315. /* Apply any externally detected TSC adjustments (due to suspend) */
  2316. if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
  2317. adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
  2318. vcpu->arch.tsc_offset_adjustment = 0;
  2319. set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
  2320. }
  2321. if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
  2322. s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
  2323. native_read_tsc() - vcpu->arch.last_host_tsc;
  2324. if (tsc_delta < 0)
  2325. mark_tsc_unstable("KVM discovered backwards TSC");
  2326. if (check_tsc_unstable()) {
  2327. u64 offset = kvm_x86_ops->compute_tsc_offset(vcpu,
  2328. vcpu->arch.last_guest_tsc);
  2329. kvm_x86_ops->write_tsc_offset(vcpu, offset);
  2330. vcpu->arch.tsc_catchup = 1;
  2331. }
  2332. /*
  2333. * On a host with synchronized TSC, there is no need to update
  2334. * kvmclock on vcpu->cpu migration
  2335. */
  2336. if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
  2337. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  2338. if (vcpu->cpu != cpu)
  2339. kvm_migrate_timers(vcpu);
  2340. vcpu->cpu = cpu;
  2341. }
  2342. accumulate_steal_time(vcpu);
  2343. kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
  2344. }
  2345. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  2346. {
  2347. kvm_x86_ops->vcpu_put(vcpu);
  2348. kvm_put_guest_fpu(vcpu);
  2349. vcpu->arch.last_host_tsc = native_read_tsc();
  2350. }
  2351. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  2352. struct kvm_lapic_state *s)
  2353. {
  2354. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  2355. return 0;
  2356. }
  2357. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  2358. struct kvm_lapic_state *s)
  2359. {
  2360. kvm_apic_post_state_restore(vcpu, s);
  2361. update_cr8_intercept(vcpu);
  2362. return 0;
  2363. }
  2364. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  2365. struct kvm_interrupt *irq)
  2366. {
  2367. if (irq->irq < 0 || irq->irq >= KVM_NR_INTERRUPTS)
  2368. return -EINVAL;
  2369. if (irqchip_in_kernel(vcpu->kvm))
  2370. return -ENXIO;
  2371. kvm_queue_interrupt(vcpu, irq->irq, false);
  2372. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2373. return 0;
  2374. }
  2375. static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
  2376. {
  2377. kvm_inject_nmi(vcpu);
  2378. return 0;
  2379. }
  2380. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  2381. struct kvm_tpr_access_ctl *tac)
  2382. {
  2383. if (tac->flags)
  2384. return -EINVAL;
  2385. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  2386. return 0;
  2387. }
  2388. static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
  2389. u64 mcg_cap)
  2390. {
  2391. int r;
  2392. unsigned bank_num = mcg_cap & 0xff, bank;
  2393. r = -EINVAL;
  2394. if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
  2395. goto out;
  2396. if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
  2397. goto out;
  2398. r = 0;
  2399. vcpu->arch.mcg_cap = mcg_cap;
  2400. /* Init IA32_MCG_CTL to all 1s */
  2401. if (mcg_cap & MCG_CTL_P)
  2402. vcpu->arch.mcg_ctl = ~(u64)0;
  2403. /* Init IA32_MCi_CTL to all 1s */
  2404. for (bank = 0; bank < bank_num; bank++)
  2405. vcpu->arch.mce_banks[bank*4] = ~(u64)0;
  2406. out:
  2407. return r;
  2408. }
  2409. static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
  2410. struct kvm_x86_mce *mce)
  2411. {
  2412. u64 mcg_cap = vcpu->arch.mcg_cap;
  2413. unsigned bank_num = mcg_cap & 0xff;
  2414. u64 *banks = vcpu->arch.mce_banks;
  2415. if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
  2416. return -EINVAL;
  2417. /*
  2418. * if IA32_MCG_CTL is not all 1s, the uncorrected error
  2419. * reporting is disabled
  2420. */
  2421. if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
  2422. vcpu->arch.mcg_ctl != ~(u64)0)
  2423. return 0;
  2424. banks += 4 * mce->bank;
  2425. /*
  2426. * if IA32_MCi_CTL is not all 1s, the uncorrected error
  2427. * reporting is disabled for the bank
  2428. */
  2429. if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
  2430. return 0;
  2431. if (mce->status & MCI_STATUS_UC) {
  2432. if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
  2433. !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
  2434. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2435. return 0;
  2436. }
  2437. if (banks[1] & MCI_STATUS_VAL)
  2438. mce->status |= MCI_STATUS_OVER;
  2439. banks[2] = mce->addr;
  2440. banks[3] = mce->misc;
  2441. vcpu->arch.mcg_status = mce->mcg_status;
  2442. banks[1] = mce->status;
  2443. kvm_queue_exception(vcpu, MC_VECTOR);
  2444. } else if (!(banks[1] & MCI_STATUS_VAL)
  2445. || !(banks[1] & MCI_STATUS_UC)) {
  2446. if (banks[1] & MCI_STATUS_VAL)
  2447. mce->status |= MCI_STATUS_OVER;
  2448. banks[2] = mce->addr;
  2449. banks[3] = mce->misc;
  2450. banks[1] = mce->status;
  2451. } else
  2452. banks[1] |= MCI_STATUS_OVER;
  2453. return 0;
  2454. }
  2455. static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
  2456. struct kvm_vcpu_events *events)
  2457. {
  2458. process_nmi(vcpu);
  2459. events->exception.injected =
  2460. vcpu->arch.exception.pending &&
  2461. !kvm_exception_is_soft(vcpu->arch.exception.nr);
  2462. events->exception.nr = vcpu->arch.exception.nr;
  2463. events->exception.has_error_code = vcpu->arch.exception.has_error_code;
  2464. events->exception.pad = 0;
  2465. events->exception.error_code = vcpu->arch.exception.error_code;
  2466. events->interrupt.injected =
  2467. vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
  2468. events->interrupt.nr = vcpu->arch.interrupt.nr;
  2469. events->interrupt.soft = 0;
  2470. events->interrupt.shadow =
  2471. kvm_x86_ops->get_interrupt_shadow(vcpu,
  2472. KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
  2473. events->nmi.injected = vcpu->arch.nmi_injected;
  2474. events->nmi.pending = vcpu->arch.nmi_pending != 0;
  2475. events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
  2476. events->nmi.pad = 0;
  2477. events->sipi_vector = vcpu->arch.sipi_vector;
  2478. events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
  2479. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  2480. | KVM_VCPUEVENT_VALID_SHADOW);
  2481. memset(&events->reserved, 0, sizeof(events->reserved));
  2482. }
  2483. static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
  2484. struct kvm_vcpu_events *events)
  2485. {
  2486. if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
  2487. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  2488. | KVM_VCPUEVENT_VALID_SHADOW))
  2489. return -EINVAL;
  2490. process_nmi(vcpu);
  2491. vcpu->arch.exception.pending = events->exception.injected;
  2492. vcpu->arch.exception.nr = events->exception.nr;
  2493. vcpu->arch.exception.has_error_code = events->exception.has_error_code;
  2494. vcpu->arch.exception.error_code = events->exception.error_code;
  2495. vcpu->arch.interrupt.pending = events->interrupt.injected;
  2496. vcpu->arch.interrupt.nr = events->interrupt.nr;
  2497. vcpu->arch.interrupt.soft = events->interrupt.soft;
  2498. if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
  2499. kvm_x86_ops->set_interrupt_shadow(vcpu,
  2500. events->interrupt.shadow);
  2501. vcpu->arch.nmi_injected = events->nmi.injected;
  2502. if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
  2503. vcpu->arch.nmi_pending = events->nmi.pending;
  2504. kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
  2505. if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
  2506. vcpu->arch.sipi_vector = events->sipi_vector;
  2507. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2508. return 0;
  2509. }
  2510. static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
  2511. struct kvm_debugregs *dbgregs)
  2512. {
  2513. memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
  2514. dbgregs->dr6 = vcpu->arch.dr6;
  2515. dbgregs->dr7 = vcpu->arch.dr7;
  2516. dbgregs->flags = 0;
  2517. memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
  2518. }
  2519. static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
  2520. struct kvm_debugregs *dbgregs)
  2521. {
  2522. if (dbgregs->flags)
  2523. return -EINVAL;
  2524. memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
  2525. vcpu->arch.dr6 = dbgregs->dr6;
  2526. vcpu->arch.dr7 = dbgregs->dr7;
  2527. return 0;
  2528. }
  2529. static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
  2530. struct kvm_xsave *guest_xsave)
  2531. {
  2532. if (cpu_has_xsave)
  2533. memcpy(guest_xsave->region,
  2534. &vcpu->arch.guest_fpu.state->xsave,
  2535. xstate_size);
  2536. else {
  2537. memcpy(guest_xsave->region,
  2538. &vcpu->arch.guest_fpu.state->fxsave,
  2539. sizeof(struct i387_fxsave_struct));
  2540. *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
  2541. XSTATE_FPSSE;
  2542. }
  2543. }
  2544. static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
  2545. struct kvm_xsave *guest_xsave)
  2546. {
  2547. u64 xstate_bv =
  2548. *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
  2549. if (cpu_has_xsave)
  2550. memcpy(&vcpu->arch.guest_fpu.state->xsave,
  2551. guest_xsave->region, xstate_size);
  2552. else {
  2553. if (xstate_bv & ~XSTATE_FPSSE)
  2554. return -EINVAL;
  2555. memcpy(&vcpu->arch.guest_fpu.state->fxsave,
  2556. guest_xsave->region, sizeof(struct i387_fxsave_struct));
  2557. }
  2558. return 0;
  2559. }
  2560. static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
  2561. struct kvm_xcrs *guest_xcrs)
  2562. {
  2563. if (!cpu_has_xsave) {
  2564. guest_xcrs->nr_xcrs = 0;
  2565. return;
  2566. }
  2567. guest_xcrs->nr_xcrs = 1;
  2568. guest_xcrs->flags = 0;
  2569. guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
  2570. guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
  2571. }
  2572. static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
  2573. struct kvm_xcrs *guest_xcrs)
  2574. {
  2575. int i, r = 0;
  2576. if (!cpu_has_xsave)
  2577. return -EINVAL;
  2578. if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
  2579. return -EINVAL;
  2580. for (i = 0; i < guest_xcrs->nr_xcrs; i++)
  2581. /* Only support XCR0 currently */
  2582. if (guest_xcrs->xcrs[0].xcr == XCR_XFEATURE_ENABLED_MASK) {
  2583. r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
  2584. guest_xcrs->xcrs[0].value);
  2585. break;
  2586. }
  2587. if (r)
  2588. r = -EINVAL;
  2589. return r;
  2590. }
  2591. /*
  2592. * kvm_set_guest_paused() indicates to the guest kernel that it has been
  2593. * stopped by the hypervisor. This function will be called from the host only.
  2594. * EINVAL is returned when the host attempts to set the flag for a guest that
  2595. * does not support pv clocks.
  2596. */
  2597. static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
  2598. {
  2599. if (!vcpu->arch.time_page)
  2600. return -EINVAL;
  2601. vcpu->arch.pvclock_set_guest_stopped_request = true;
  2602. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  2603. return 0;
  2604. }
  2605. long kvm_arch_vcpu_ioctl(struct file *filp,
  2606. unsigned int ioctl, unsigned long arg)
  2607. {
  2608. struct kvm_vcpu *vcpu = filp->private_data;
  2609. void __user *argp = (void __user *)arg;
  2610. int r;
  2611. union {
  2612. struct kvm_lapic_state *lapic;
  2613. struct kvm_xsave *xsave;
  2614. struct kvm_xcrs *xcrs;
  2615. void *buffer;
  2616. } u;
  2617. u.buffer = NULL;
  2618. switch (ioctl) {
  2619. case KVM_GET_LAPIC: {
  2620. r = -EINVAL;
  2621. if (!vcpu->arch.apic)
  2622. goto out;
  2623. u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  2624. r = -ENOMEM;
  2625. if (!u.lapic)
  2626. goto out;
  2627. r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
  2628. if (r)
  2629. goto out;
  2630. r = -EFAULT;
  2631. if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
  2632. goto out;
  2633. r = 0;
  2634. break;
  2635. }
  2636. case KVM_SET_LAPIC: {
  2637. r = -EINVAL;
  2638. if (!vcpu->arch.apic)
  2639. goto out;
  2640. u.lapic = memdup_user(argp, sizeof(*u.lapic));
  2641. if (IS_ERR(u.lapic))
  2642. return PTR_ERR(u.lapic);
  2643. r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
  2644. break;
  2645. }
  2646. case KVM_INTERRUPT: {
  2647. struct kvm_interrupt irq;
  2648. r = -EFAULT;
  2649. if (copy_from_user(&irq, argp, sizeof irq))
  2650. goto out;
  2651. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2652. break;
  2653. }
  2654. case KVM_NMI: {
  2655. r = kvm_vcpu_ioctl_nmi(vcpu);
  2656. break;
  2657. }
  2658. case KVM_SET_CPUID: {
  2659. struct kvm_cpuid __user *cpuid_arg = argp;
  2660. struct kvm_cpuid cpuid;
  2661. r = -EFAULT;
  2662. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2663. goto out;
  2664. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2665. break;
  2666. }
  2667. case KVM_SET_CPUID2: {
  2668. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2669. struct kvm_cpuid2 cpuid;
  2670. r = -EFAULT;
  2671. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2672. goto out;
  2673. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  2674. cpuid_arg->entries);
  2675. break;
  2676. }
  2677. case KVM_GET_CPUID2: {
  2678. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2679. struct kvm_cpuid2 cpuid;
  2680. r = -EFAULT;
  2681. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2682. goto out;
  2683. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  2684. cpuid_arg->entries);
  2685. if (r)
  2686. goto out;
  2687. r = -EFAULT;
  2688. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  2689. goto out;
  2690. r = 0;
  2691. break;
  2692. }
  2693. case KVM_GET_MSRS:
  2694. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2695. break;
  2696. case KVM_SET_MSRS:
  2697. r = msr_io(vcpu, argp, do_set_msr, 0);
  2698. break;
  2699. case KVM_TPR_ACCESS_REPORTING: {
  2700. struct kvm_tpr_access_ctl tac;
  2701. r = -EFAULT;
  2702. if (copy_from_user(&tac, argp, sizeof tac))
  2703. goto out;
  2704. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  2705. if (r)
  2706. goto out;
  2707. r = -EFAULT;
  2708. if (copy_to_user(argp, &tac, sizeof tac))
  2709. goto out;
  2710. r = 0;
  2711. break;
  2712. };
  2713. case KVM_SET_VAPIC_ADDR: {
  2714. struct kvm_vapic_addr va;
  2715. r = -EINVAL;
  2716. if (!irqchip_in_kernel(vcpu->kvm))
  2717. goto out;
  2718. r = -EFAULT;
  2719. if (copy_from_user(&va, argp, sizeof va))
  2720. goto out;
  2721. r = 0;
  2722. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  2723. break;
  2724. }
  2725. case KVM_X86_SETUP_MCE: {
  2726. u64 mcg_cap;
  2727. r = -EFAULT;
  2728. if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
  2729. goto out;
  2730. r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
  2731. break;
  2732. }
  2733. case KVM_X86_SET_MCE: {
  2734. struct kvm_x86_mce mce;
  2735. r = -EFAULT;
  2736. if (copy_from_user(&mce, argp, sizeof mce))
  2737. goto out;
  2738. r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
  2739. break;
  2740. }
  2741. case KVM_GET_VCPU_EVENTS: {
  2742. struct kvm_vcpu_events events;
  2743. kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
  2744. r = -EFAULT;
  2745. if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
  2746. break;
  2747. r = 0;
  2748. break;
  2749. }
  2750. case KVM_SET_VCPU_EVENTS: {
  2751. struct kvm_vcpu_events events;
  2752. r = -EFAULT;
  2753. if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
  2754. break;
  2755. r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
  2756. break;
  2757. }
  2758. case KVM_GET_DEBUGREGS: {
  2759. struct kvm_debugregs dbgregs;
  2760. kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
  2761. r = -EFAULT;
  2762. if (copy_to_user(argp, &dbgregs,
  2763. sizeof(struct kvm_debugregs)))
  2764. break;
  2765. r = 0;
  2766. break;
  2767. }
  2768. case KVM_SET_DEBUGREGS: {
  2769. struct kvm_debugregs dbgregs;
  2770. r = -EFAULT;
  2771. if (copy_from_user(&dbgregs, argp,
  2772. sizeof(struct kvm_debugregs)))
  2773. break;
  2774. r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
  2775. break;
  2776. }
  2777. case KVM_GET_XSAVE: {
  2778. u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
  2779. r = -ENOMEM;
  2780. if (!u.xsave)
  2781. break;
  2782. kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
  2783. r = -EFAULT;
  2784. if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
  2785. break;
  2786. r = 0;
  2787. break;
  2788. }
  2789. case KVM_SET_XSAVE: {
  2790. u.xsave = memdup_user(argp, sizeof(*u.xsave));
  2791. if (IS_ERR(u.xsave))
  2792. return PTR_ERR(u.xsave);
  2793. r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
  2794. break;
  2795. }
  2796. case KVM_GET_XCRS: {
  2797. u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
  2798. r = -ENOMEM;
  2799. if (!u.xcrs)
  2800. break;
  2801. kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
  2802. r = -EFAULT;
  2803. if (copy_to_user(argp, u.xcrs,
  2804. sizeof(struct kvm_xcrs)))
  2805. break;
  2806. r = 0;
  2807. break;
  2808. }
  2809. case KVM_SET_XCRS: {
  2810. u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
  2811. if (IS_ERR(u.xcrs))
  2812. return PTR_ERR(u.xcrs);
  2813. r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
  2814. break;
  2815. }
  2816. case KVM_SET_TSC_KHZ: {
  2817. u32 user_tsc_khz;
  2818. r = -EINVAL;
  2819. user_tsc_khz = (u32)arg;
  2820. if (user_tsc_khz >= kvm_max_guest_tsc_khz)
  2821. goto out;
  2822. if (user_tsc_khz == 0)
  2823. user_tsc_khz = tsc_khz;
  2824. kvm_set_tsc_khz(vcpu, user_tsc_khz);
  2825. r = 0;
  2826. goto out;
  2827. }
  2828. case KVM_GET_TSC_KHZ: {
  2829. r = vcpu->arch.virtual_tsc_khz;
  2830. goto out;
  2831. }
  2832. case KVM_KVMCLOCK_CTRL: {
  2833. r = kvm_set_guest_paused(vcpu);
  2834. goto out;
  2835. }
  2836. default:
  2837. r = -EINVAL;
  2838. }
  2839. out:
  2840. kfree(u.buffer);
  2841. return r;
  2842. }
  2843. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  2844. {
  2845. return VM_FAULT_SIGBUS;
  2846. }
  2847. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  2848. {
  2849. int ret;
  2850. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  2851. return -EINVAL;
  2852. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  2853. return ret;
  2854. }
  2855. static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
  2856. u64 ident_addr)
  2857. {
  2858. kvm->arch.ept_identity_map_addr = ident_addr;
  2859. return 0;
  2860. }
  2861. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  2862. u32 kvm_nr_mmu_pages)
  2863. {
  2864. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  2865. return -EINVAL;
  2866. mutex_lock(&kvm->slots_lock);
  2867. spin_lock(&kvm->mmu_lock);
  2868. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  2869. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  2870. spin_unlock(&kvm->mmu_lock);
  2871. mutex_unlock(&kvm->slots_lock);
  2872. return 0;
  2873. }
  2874. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  2875. {
  2876. return kvm->arch.n_max_mmu_pages;
  2877. }
  2878. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2879. {
  2880. int r;
  2881. r = 0;
  2882. switch (chip->chip_id) {
  2883. case KVM_IRQCHIP_PIC_MASTER:
  2884. memcpy(&chip->chip.pic,
  2885. &pic_irqchip(kvm)->pics[0],
  2886. sizeof(struct kvm_pic_state));
  2887. break;
  2888. case KVM_IRQCHIP_PIC_SLAVE:
  2889. memcpy(&chip->chip.pic,
  2890. &pic_irqchip(kvm)->pics[1],
  2891. sizeof(struct kvm_pic_state));
  2892. break;
  2893. case KVM_IRQCHIP_IOAPIC:
  2894. r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
  2895. break;
  2896. default:
  2897. r = -EINVAL;
  2898. break;
  2899. }
  2900. return r;
  2901. }
  2902. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2903. {
  2904. int r;
  2905. r = 0;
  2906. switch (chip->chip_id) {
  2907. case KVM_IRQCHIP_PIC_MASTER:
  2908. spin_lock(&pic_irqchip(kvm)->lock);
  2909. memcpy(&pic_irqchip(kvm)->pics[0],
  2910. &chip->chip.pic,
  2911. sizeof(struct kvm_pic_state));
  2912. spin_unlock(&pic_irqchip(kvm)->lock);
  2913. break;
  2914. case KVM_IRQCHIP_PIC_SLAVE:
  2915. spin_lock(&pic_irqchip(kvm)->lock);
  2916. memcpy(&pic_irqchip(kvm)->pics[1],
  2917. &chip->chip.pic,
  2918. sizeof(struct kvm_pic_state));
  2919. spin_unlock(&pic_irqchip(kvm)->lock);
  2920. break;
  2921. case KVM_IRQCHIP_IOAPIC:
  2922. r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
  2923. break;
  2924. default:
  2925. r = -EINVAL;
  2926. break;
  2927. }
  2928. kvm_pic_update_irq(pic_irqchip(kvm));
  2929. return r;
  2930. }
  2931. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2932. {
  2933. int r = 0;
  2934. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2935. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  2936. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2937. return r;
  2938. }
  2939. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2940. {
  2941. int r = 0;
  2942. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2943. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  2944. kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
  2945. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2946. return r;
  2947. }
  2948. static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2949. {
  2950. int r = 0;
  2951. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2952. memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
  2953. sizeof(ps->channels));
  2954. ps->flags = kvm->arch.vpit->pit_state.flags;
  2955. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2956. memset(&ps->reserved, 0, sizeof(ps->reserved));
  2957. return r;
  2958. }
  2959. static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2960. {
  2961. int r = 0, start = 0;
  2962. u32 prev_legacy, cur_legacy;
  2963. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2964. prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2965. cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2966. if (!prev_legacy && cur_legacy)
  2967. start = 1;
  2968. memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
  2969. sizeof(kvm->arch.vpit->pit_state.channels));
  2970. kvm->arch.vpit->pit_state.flags = ps->flags;
  2971. kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
  2972. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2973. return r;
  2974. }
  2975. static int kvm_vm_ioctl_reinject(struct kvm *kvm,
  2976. struct kvm_reinject_control *control)
  2977. {
  2978. if (!kvm->arch.vpit)
  2979. return -ENXIO;
  2980. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2981. kvm->arch.vpit->pit_state.reinject = control->pit_reinject;
  2982. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2983. return 0;
  2984. }
  2985. /**
  2986. * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
  2987. * @kvm: kvm instance
  2988. * @log: slot id and address to which we copy the log
  2989. *
  2990. * We need to keep it in mind that VCPU threads can write to the bitmap
  2991. * concurrently. So, to avoid losing data, we keep the following order for
  2992. * each bit:
  2993. *
  2994. * 1. Take a snapshot of the bit and clear it if needed.
  2995. * 2. Write protect the corresponding page.
  2996. * 3. Flush TLB's if needed.
  2997. * 4. Copy the snapshot to the userspace.
  2998. *
  2999. * Between 2 and 3, the guest may write to the page using the remaining TLB
  3000. * entry. This is not a problem because the page will be reported dirty at
  3001. * step 4 using the snapshot taken before and step 3 ensures that successive
  3002. * writes will be logged for the next call.
  3003. */
  3004. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  3005. {
  3006. int r;
  3007. struct kvm_memory_slot *memslot;
  3008. unsigned long n, i;
  3009. unsigned long *dirty_bitmap;
  3010. unsigned long *dirty_bitmap_buffer;
  3011. bool is_dirty = false;
  3012. mutex_lock(&kvm->slots_lock);
  3013. r = -EINVAL;
  3014. if (log->slot >= KVM_MEMORY_SLOTS)
  3015. goto out;
  3016. memslot = id_to_memslot(kvm->memslots, log->slot);
  3017. dirty_bitmap = memslot->dirty_bitmap;
  3018. r = -ENOENT;
  3019. if (!dirty_bitmap)
  3020. goto out;
  3021. n = kvm_dirty_bitmap_bytes(memslot);
  3022. dirty_bitmap_buffer = dirty_bitmap + n / sizeof(long);
  3023. memset(dirty_bitmap_buffer, 0, n);
  3024. spin_lock(&kvm->mmu_lock);
  3025. for (i = 0; i < n / sizeof(long); i++) {
  3026. unsigned long mask;
  3027. gfn_t offset;
  3028. if (!dirty_bitmap[i])
  3029. continue;
  3030. is_dirty = true;
  3031. mask = xchg(&dirty_bitmap[i], 0);
  3032. dirty_bitmap_buffer[i] = mask;
  3033. offset = i * BITS_PER_LONG;
  3034. kvm_mmu_write_protect_pt_masked(kvm, memslot, offset, mask);
  3035. }
  3036. if (is_dirty)
  3037. kvm_flush_remote_tlbs(kvm);
  3038. spin_unlock(&kvm->mmu_lock);
  3039. r = -EFAULT;
  3040. if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
  3041. goto out;
  3042. r = 0;
  3043. out:
  3044. mutex_unlock(&kvm->slots_lock);
  3045. return r;
  3046. }
  3047. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event)
  3048. {
  3049. if (!irqchip_in_kernel(kvm))
  3050. return -ENXIO;
  3051. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  3052. irq_event->irq, irq_event->level);
  3053. return 0;
  3054. }
  3055. long kvm_arch_vm_ioctl(struct file *filp,
  3056. unsigned int ioctl, unsigned long arg)
  3057. {
  3058. struct kvm *kvm = filp->private_data;
  3059. void __user *argp = (void __user *)arg;
  3060. int r = -ENOTTY;
  3061. /*
  3062. * This union makes it completely explicit to gcc-3.x
  3063. * that these two variables' stack usage should be
  3064. * combined, not added together.
  3065. */
  3066. union {
  3067. struct kvm_pit_state ps;
  3068. struct kvm_pit_state2 ps2;
  3069. struct kvm_pit_config pit_config;
  3070. } u;
  3071. switch (ioctl) {
  3072. case KVM_SET_TSS_ADDR:
  3073. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  3074. break;
  3075. case KVM_SET_IDENTITY_MAP_ADDR: {
  3076. u64 ident_addr;
  3077. r = -EFAULT;
  3078. if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
  3079. goto out;
  3080. r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
  3081. break;
  3082. }
  3083. case KVM_SET_NR_MMU_PAGES:
  3084. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  3085. break;
  3086. case KVM_GET_NR_MMU_PAGES:
  3087. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  3088. break;
  3089. case KVM_CREATE_IRQCHIP: {
  3090. struct kvm_pic *vpic;
  3091. mutex_lock(&kvm->lock);
  3092. r = -EEXIST;
  3093. if (kvm->arch.vpic)
  3094. goto create_irqchip_unlock;
  3095. r = -EINVAL;
  3096. if (atomic_read(&kvm->online_vcpus))
  3097. goto create_irqchip_unlock;
  3098. r = -ENOMEM;
  3099. vpic = kvm_create_pic(kvm);
  3100. if (vpic) {
  3101. r = kvm_ioapic_init(kvm);
  3102. if (r) {
  3103. mutex_lock(&kvm->slots_lock);
  3104. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  3105. &vpic->dev_master);
  3106. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  3107. &vpic->dev_slave);
  3108. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  3109. &vpic->dev_eclr);
  3110. mutex_unlock(&kvm->slots_lock);
  3111. kfree(vpic);
  3112. goto create_irqchip_unlock;
  3113. }
  3114. } else
  3115. goto create_irqchip_unlock;
  3116. smp_wmb();
  3117. kvm->arch.vpic = vpic;
  3118. smp_wmb();
  3119. r = kvm_setup_default_irq_routing(kvm);
  3120. if (r) {
  3121. mutex_lock(&kvm->slots_lock);
  3122. mutex_lock(&kvm->irq_lock);
  3123. kvm_ioapic_destroy(kvm);
  3124. kvm_destroy_pic(kvm);
  3125. mutex_unlock(&kvm->irq_lock);
  3126. mutex_unlock(&kvm->slots_lock);
  3127. }
  3128. create_irqchip_unlock:
  3129. mutex_unlock(&kvm->lock);
  3130. break;
  3131. }
  3132. case KVM_CREATE_PIT:
  3133. u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
  3134. goto create_pit;
  3135. case KVM_CREATE_PIT2:
  3136. r = -EFAULT;
  3137. if (copy_from_user(&u.pit_config, argp,
  3138. sizeof(struct kvm_pit_config)))
  3139. goto out;
  3140. create_pit:
  3141. mutex_lock(&kvm->slots_lock);
  3142. r = -EEXIST;
  3143. if (kvm->arch.vpit)
  3144. goto create_pit_unlock;
  3145. r = -ENOMEM;
  3146. kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
  3147. if (kvm->arch.vpit)
  3148. r = 0;
  3149. create_pit_unlock:
  3150. mutex_unlock(&kvm->slots_lock);
  3151. break;
  3152. case KVM_GET_IRQCHIP: {
  3153. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  3154. struct kvm_irqchip *chip;
  3155. chip = memdup_user(argp, sizeof(*chip));
  3156. if (IS_ERR(chip)) {
  3157. r = PTR_ERR(chip);
  3158. goto out;
  3159. }
  3160. r = -ENXIO;
  3161. if (!irqchip_in_kernel(kvm))
  3162. goto get_irqchip_out;
  3163. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  3164. if (r)
  3165. goto get_irqchip_out;
  3166. r = -EFAULT;
  3167. if (copy_to_user(argp, chip, sizeof *chip))
  3168. goto get_irqchip_out;
  3169. r = 0;
  3170. get_irqchip_out:
  3171. kfree(chip);
  3172. break;
  3173. }
  3174. case KVM_SET_IRQCHIP: {
  3175. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  3176. struct kvm_irqchip *chip;
  3177. chip = memdup_user(argp, sizeof(*chip));
  3178. if (IS_ERR(chip)) {
  3179. r = PTR_ERR(chip);
  3180. goto out;
  3181. }
  3182. r = -ENXIO;
  3183. if (!irqchip_in_kernel(kvm))
  3184. goto set_irqchip_out;
  3185. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  3186. if (r)
  3187. goto set_irqchip_out;
  3188. r = 0;
  3189. set_irqchip_out:
  3190. kfree(chip);
  3191. break;
  3192. }
  3193. case KVM_GET_PIT: {
  3194. r = -EFAULT;
  3195. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  3196. goto out;
  3197. r = -ENXIO;
  3198. if (!kvm->arch.vpit)
  3199. goto out;
  3200. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  3201. if (r)
  3202. goto out;
  3203. r = -EFAULT;
  3204. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  3205. goto out;
  3206. r = 0;
  3207. break;
  3208. }
  3209. case KVM_SET_PIT: {
  3210. r = -EFAULT;
  3211. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  3212. goto out;
  3213. r = -ENXIO;
  3214. if (!kvm->arch.vpit)
  3215. goto out;
  3216. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  3217. break;
  3218. }
  3219. case KVM_GET_PIT2: {
  3220. r = -ENXIO;
  3221. if (!kvm->arch.vpit)
  3222. goto out;
  3223. r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
  3224. if (r)
  3225. goto out;
  3226. r = -EFAULT;
  3227. if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
  3228. goto out;
  3229. r = 0;
  3230. break;
  3231. }
  3232. case KVM_SET_PIT2: {
  3233. r = -EFAULT;
  3234. if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
  3235. goto out;
  3236. r = -ENXIO;
  3237. if (!kvm->arch.vpit)
  3238. goto out;
  3239. r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
  3240. break;
  3241. }
  3242. case KVM_REINJECT_CONTROL: {
  3243. struct kvm_reinject_control control;
  3244. r = -EFAULT;
  3245. if (copy_from_user(&control, argp, sizeof(control)))
  3246. goto out;
  3247. r = kvm_vm_ioctl_reinject(kvm, &control);
  3248. break;
  3249. }
  3250. case KVM_XEN_HVM_CONFIG: {
  3251. r = -EFAULT;
  3252. if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
  3253. sizeof(struct kvm_xen_hvm_config)))
  3254. goto out;
  3255. r = -EINVAL;
  3256. if (kvm->arch.xen_hvm_config.flags)
  3257. goto out;
  3258. r = 0;
  3259. break;
  3260. }
  3261. case KVM_SET_CLOCK: {
  3262. struct kvm_clock_data user_ns;
  3263. u64 now_ns;
  3264. s64 delta;
  3265. r = -EFAULT;
  3266. if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
  3267. goto out;
  3268. r = -EINVAL;
  3269. if (user_ns.flags)
  3270. goto out;
  3271. r = 0;
  3272. local_irq_disable();
  3273. now_ns = get_kernel_ns();
  3274. delta = user_ns.clock - now_ns;
  3275. local_irq_enable();
  3276. kvm->arch.kvmclock_offset = delta;
  3277. break;
  3278. }
  3279. case KVM_GET_CLOCK: {
  3280. struct kvm_clock_data user_ns;
  3281. u64 now_ns;
  3282. local_irq_disable();
  3283. now_ns = get_kernel_ns();
  3284. user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
  3285. local_irq_enable();
  3286. user_ns.flags = 0;
  3287. memset(&user_ns.pad, 0, sizeof(user_ns.pad));
  3288. r = -EFAULT;
  3289. if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
  3290. goto out;
  3291. r = 0;
  3292. break;
  3293. }
  3294. default:
  3295. ;
  3296. }
  3297. out:
  3298. return r;
  3299. }
  3300. static void kvm_init_msr_list(void)
  3301. {
  3302. u32 dummy[2];
  3303. unsigned i, j;
  3304. /* skip the first msrs in the list. KVM-specific */
  3305. for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
  3306. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  3307. continue;
  3308. if (j < i)
  3309. msrs_to_save[j] = msrs_to_save[i];
  3310. j++;
  3311. }
  3312. num_msrs_to_save = j;
  3313. }
  3314. static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
  3315. const void *v)
  3316. {
  3317. int handled = 0;
  3318. int n;
  3319. do {
  3320. n = min(len, 8);
  3321. if (!(vcpu->arch.apic &&
  3322. !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, n, v))
  3323. && kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
  3324. break;
  3325. handled += n;
  3326. addr += n;
  3327. len -= n;
  3328. v += n;
  3329. } while (len);
  3330. return handled;
  3331. }
  3332. static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
  3333. {
  3334. int handled = 0;
  3335. int n;
  3336. do {
  3337. n = min(len, 8);
  3338. if (!(vcpu->arch.apic &&
  3339. !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, n, v))
  3340. && kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
  3341. break;
  3342. trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
  3343. handled += n;
  3344. addr += n;
  3345. len -= n;
  3346. v += n;
  3347. } while (len);
  3348. return handled;
  3349. }
  3350. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  3351. struct kvm_segment *var, int seg)
  3352. {
  3353. kvm_x86_ops->set_segment(vcpu, var, seg);
  3354. }
  3355. void kvm_get_segment(struct kvm_vcpu *vcpu,
  3356. struct kvm_segment *var, int seg)
  3357. {
  3358. kvm_x86_ops->get_segment(vcpu, var, seg);
  3359. }
  3360. gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access)
  3361. {
  3362. gpa_t t_gpa;
  3363. struct x86_exception exception;
  3364. BUG_ON(!mmu_is_nested(vcpu));
  3365. /* NPT walks are always user-walks */
  3366. access |= PFERR_USER_MASK;
  3367. t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, &exception);
  3368. return t_gpa;
  3369. }
  3370. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
  3371. struct x86_exception *exception)
  3372. {
  3373. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3374. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3375. }
  3376. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
  3377. struct x86_exception *exception)
  3378. {
  3379. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3380. access |= PFERR_FETCH_MASK;
  3381. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3382. }
  3383. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
  3384. struct x86_exception *exception)
  3385. {
  3386. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3387. access |= PFERR_WRITE_MASK;
  3388. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3389. }
  3390. /* uses this to access any guest's mapped memory without checking CPL */
  3391. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
  3392. struct x86_exception *exception)
  3393. {
  3394. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
  3395. }
  3396. static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
  3397. struct kvm_vcpu *vcpu, u32 access,
  3398. struct x86_exception *exception)
  3399. {
  3400. void *data = val;
  3401. int r = X86EMUL_CONTINUE;
  3402. while (bytes) {
  3403. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
  3404. exception);
  3405. unsigned offset = addr & (PAGE_SIZE-1);
  3406. unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
  3407. int ret;
  3408. if (gpa == UNMAPPED_GVA)
  3409. return X86EMUL_PROPAGATE_FAULT;
  3410. ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
  3411. if (ret < 0) {
  3412. r = X86EMUL_IO_NEEDED;
  3413. goto out;
  3414. }
  3415. bytes -= toread;
  3416. data += toread;
  3417. addr += toread;
  3418. }
  3419. out:
  3420. return r;
  3421. }
  3422. /* used for instruction fetching */
  3423. static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
  3424. gva_t addr, void *val, unsigned int bytes,
  3425. struct x86_exception *exception)
  3426. {
  3427. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3428. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3429. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
  3430. access | PFERR_FETCH_MASK,
  3431. exception);
  3432. }
  3433. int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
  3434. gva_t addr, void *val, unsigned int bytes,
  3435. struct x86_exception *exception)
  3436. {
  3437. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3438. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3439. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
  3440. exception);
  3441. }
  3442. EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
  3443. static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
  3444. gva_t addr, void *val, unsigned int bytes,
  3445. struct x86_exception *exception)
  3446. {
  3447. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3448. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
  3449. }
  3450. int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
  3451. gva_t addr, void *val,
  3452. unsigned int bytes,
  3453. struct x86_exception *exception)
  3454. {
  3455. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3456. void *data = val;
  3457. int r = X86EMUL_CONTINUE;
  3458. while (bytes) {
  3459. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
  3460. PFERR_WRITE_MASK,
  3461. exception);
  3462. unsigned offset = addr & (PAGE_SIZE-1);
  3463. unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
  3464. int ret;
  3465. if (gpa == UNMAPPED_GVA)
  3466. return X86EMUL_PROPAGATE_FAULT;
  3467. ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
  3468. if (ret < 0) {
  3469. r = X86EMUL_IO_NEEDED;
  3470. goto out;
  3471. }
  3472. bytes -= towrite;
  3473. data += towrite;
  3474. addr += towrite;
  3475. }
  3476. out:
  3477. return r;
  3478. }
  3479. EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
  3480. static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
  3481. gpa_t *gpa, struct x86_exception *exception,
  3482. bool write)
  3483. {
  3484. u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
  3485. | (write ? PFERR_WRITE_MASK : 0);
  3486. if (vcpu_match_mmio_gva(vcpu, gva)
  3487. && !permission_fault(vcpu->arch.walk_mmu, vcpu->arch.access, access)) {
  3488. *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
  3489. (gva & (PAGE_SIZE - 1));
  3490. trace_vcpu_match_mmio(gva, *gpa, write, false);
  3491. return 1;
  3492. }
  3493. *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3494. if (*gpa == UNMAPPED_GVA)
  3495. return -1;
  3496. /* For APIC access vmexit */
  3497. if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3498. return 1;
  3499. if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
  3500. trace_vcpu_match_mmio(gva, *gpa, write, true);
  3501. return 1;
  3502. }
  3503. return 0;
  3504. }
  3505. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  3506. const void *val, int bytes)
  3507. {
  3508. int ret;
  3509. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  3510. if (ret < 0)
  3511. return 0;
  3512. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  3513. return 1;
  3514. }
  3515. struct read_write_emulator_ops {
  3516. int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
  3517. int bytes);
  3518. int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3519. void *val, int bytes);
  3520. int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3521. int bytes, void *val);
  3522. int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3523. void *val, int bytes);
  3524. bool write;
  3525. };
  3526. static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
  3527. {
  3528. if (vcpu->mmio_read_completed) {
  3529. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
  3530. vcpu->mmio_fragments[0].gpa, *(u64 *)val);
  3531. vcpu->mmio_read_completed = 0;
  3532. return 1;
  3533. }
  3534. return 0;
  3535. }
  3536. static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
  3537. void *val, int bytes)
  3538. {
  3539. return !kvm_read_guest(vcpu->kvm, gpa, val, bytes);
  3540. }
  3541. static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
  3542. void *val, int bytes)
  3543. {
  3544. return emulator_write_phys(vcpu, gpa, val, bytes);
  3545. }
  3546. static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
  3547. {
  3548. trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
  3549. return vcpu_mmio_write(vcpu, gpa, bytes, val);
  3550. }
  3551. static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
  3552. void *val, int bytes)
  3553. {
  3554. trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
  3555. return X86EMUL_IO_NEEDED;
  3556. }
  3557. static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
  3558. void *val, int bytes)
  3559. {
  3560. struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
  3561. memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
  3562. return X86EMUL_CONTINUE;
  3563. }
  3564. static const struct read_write_emulator_ops read_emultor = {
  3565. .read_write_prepare = read_prepare,
  3566. .read_write_emulate = read_emulate,
  3567. .read_write_mmio = vcpu_mmio_read,
  3568. .read_write_exit_mmio = read_exit_mmio,
  3569. };
  3570. static const struct read_write_emulator_ops write_emultor = {
  3571. .read_write_emulate = write_emulate,
  3572. .read_write_mmio = write_mmio,
  3573. .read_write_exit_mmio = write_exit_mmio,
  3574. .write = true,
  3575. };
  3576. static int emulator_read_write_onepage(unsigned long addr, void *val,
  3577. unsigned int bytes,
  3578. struct x86_exception *exception,
  3579. struct kvm_vcpu *vcpu,
  3580. const struct read_write_emulator_ops *ops)
  3581. {
  3582. gpa_t gpa;
  3583. int handled, ret;
  3584. bool write = ops->write;
  3585. struct kvm_mmio_fragment *frag;
  3586. ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
  3587. if (ret < 0)
  3588. return X86EMUL_PROPAGATE_FAULT;
  3589. /* For APIC access vmexit */
  3590. if (ret)
  3591. goto mmio;
  3592. if (ops->read_write_emulate(vcpu, gpa, val, bytes))
  3593. return X86EMUL_CONTINUE;
  3594. mmio:
  3595. /*
  3596. * Is this MMIO handled locally?
  3597. */
  3598. handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
  3599. if (handled == bytes)
  3600. return X86EMUL_CONTINUE;
  3601. gpa += handled;
  3602. bytes -= handled;
  3603. val += handled;
  3604. WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
  3605. frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
  3606. frag->gpa = gpa;
  3607. frag->data = val;
  3608. frag->len = bytes;
  3609. return X86EMUL_CONTINUE;
  3610. }
  3611. int emulator_read_write(struct x86_emulate_ctxt *ctxt, unsigned long addr,
  3612. void *val, unsigned int bytes,
  3613. struct x86_exception *exception,
  3614. const struct read_write_emulator_ops *ops)
  3615. {
  3616. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3617. gpa_t gpa;
  3618. int rc;
  3619. if (ops->read_write_prepare &&
  3620. ops->read_write_prepare(vcpu, val, bytes))
  3621. return X86EMUL_CONTINUE;
  3622. vcpu->mmio_nr_fragments = 0;
  3623. /* Crossing a page boundary? */
  3624. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  3625. int now;
  3626. now = -addr & ~PAGE_MASK;
  3627. rc = emulator_read_write_onepage(addr, val, now, exception,
  3628. vcpu, ops);
  3629. if (rc != X86EMUL_CONTINUE)
  3630. return rc;
  3631. addr += now;
  3632. val += now;
  3633. bytes -= now;
  3634. }
  3635. rc = emulator_read_write_onepage(addr, val, bytes, exception,
  3636. vcpu, ops);
  3637. if (rc != X86EMUL_CONTINUE)
  3638. return rc;
  3639. if (!vcpu->mmio_nr_fragments)
  3640. return rc;
  3641. gpa = vcpu->mmio_fragments[0].gpa;
  3642. vcpu->mmio_needed = 1;
  3643. vcpu->mmio_cur_fragment = 0;
  3644. vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
  3645. vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
  3646. vcpu->run->exit_reason = KVM_EXIT_MMIO;
  3647. vcpu->run->mmio.phys_addr = gpa;
  3648. return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
  3649. }
  3650. static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
  3651. unsigned long addr,
  3652. void *val,
  3653. unsigned int bytes,
  3654. struct x86_exception *exception)
  3655. {
  3656. return emulator_read_write(ctxt, addr, val, bytes,
  3657. exception, &read_emultor);
  3658. }
  3659. int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
  3660. unsigned long addr,
  3661. const void *val,
  3662. unsigned int bytes,
  3663. struct x86_exception *exception)
  3664. {
  3665. return emulator_read_write(ctxt, addr, (void *)val, bytes,
  3666. exception, &write_emultor);
  3667. }
  3668. #define CMPXCHG_TYPE(t, ptr, old, new) \
  3669. (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
  3670. #ifdef CONFIG_X86_64
  3671. # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
  3672. #else
  3673. # define CMPXCHG64(ptr, old, new) \
  3674. (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
  3675. #endif
  3676. static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
  3677. unsigned long addr,
  3678. const void *old,
  3679. const void *new,
  3680. unsigned int bytes,
  3681. struct x86_exception *exception)
  3682. {
  3683. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3684. gpa_t gpa;
  3685. struct page *page;
  3686. char *kaddr;
  3687. bool exchanged;
  3688. /* guests cmpxchg8b have to be emulated atomically */
  3689. if (bytes > 8 || (bytes & (bytes - 1)))
  3690. goto emul_write;
  3691. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
  3692. if (gpa == UNMAPPED_GVA ||
  3693. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3694. goto emul_write;
  3695. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  3696. goto emul_write;
  3697. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  3698. if (is_error_page(page))
  3699. goto emul_write;
  3700. kaddr = kmap_atomic(page);
  3701. kaddr += offset_in_page(gpa);
  3702. switch (bytes) {
  3703. case 1:
  3704. exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
  3705. break;
  3706. case 2:
  3707. exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
  3708. break;
  3709. case 4:
  3710. exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
  3711. break;
  3712. case 8:
  3713. exchanged = CMPXCHG64(kaddr, old, new);
  3714. break;
  3715. default:
  3716. BUG();
  3717. }
  3718. kunmap_atomic(kaddr);
  3719. kvm_release_page_dirty(page);
  3720. if (!exchanged)
  3721. return X86EMUL_CMPXCHG_FAILED;
  3722. kvm_mmu_pte_write(vcpu, gpa, new, bytes);
  3723. return X86EMUL_CONTINUE;
  3724. emul_write:
  3725. printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
  3726. return emulator_write_emulated(ctxt, addr, new, bytes, exception);
  3727. }
  3728. static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
  3729. {
  3730. /* TODO: String I/O for in kernel device */
  3731. int r;
  3732. if (vcpu->arch.pio.in)
  3733. r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
  3734. vcpu->arch.pio.size, pd);
  3735. else
  3736. r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
  3737. vcpu->arch.pio.port, vcpu->arch.pio.size,
  3738. pd);
  3739. return r;
  3740. }
  3741. static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
  3742. unsigned short port, void *val,
  3743. unsigned int count, bool in)
  3744. {
  3745. trace_kvm_pio(!in, port, size, count);
  3746. vcpu->arch.pio.port = port;
  3747. vcpu->arch.pio.in = in;
  3748. vcpu->arch.pio.count = count;
  3749. vcpu->arch.pio.size = size;
  3750. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  3751. vcpu->arch.pio.count = 0;
  3752. return 1;
  3753. }
  3754. vcpu->run->exit_reason = KVM_EXIT_IO;
  3755. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  3756. vcpu->run->io.size = size;
  3757. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  3758. vcpu->run->io.count = count;
  3759. vcpu->run->io.port = port;
  3760. return 0;
  3761. }
  3762. static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
  3763. int size, unsigned short port, void *val,
  3764. unsigned int count)
  3765. {
  3766. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3767. int ret;
  3768. if (vcpu->arch.pio.count)
  3769. goto data_avail;
  3770. ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
  3771. if (ret) {
  3772. data_avail:
  3773. memcpy(val, vcpu->arch.pio_data, size * count);
  3774. vcpu->arch.pio.count = 0;
  3775. return 1;
  3776. }
  3777. return 0;
  3778. }
  3779. static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
  3780. int size, unsigned short port,
  3781. const void *val, unsigned int count)
  3782. {
  3783. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3784. memcpy(vcpu->arch.pio_data, val, size * count);
  3785. return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
  3786. }
  3787. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3788. {
  3789. return kvm_x86_ops->get_segment_base(vcpu, seg);
  3790. }
  3791. static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
  3792. {
  3793. kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
  3794. }
  3795. int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
  3796. {
  3797. if (!need_emulate_wbinvd(vcpu))
  3798. return X86EMUL_CONTINUE;
  3799. if (kvm_x86_ops->has_wbinvd_exit()) {
  3800. int cpu = get_cpu();
  3801. cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
  3802. smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
  3803. wbinvd_ipi, NULL, 1);
  3804. put_cpu();
  3805. cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
  3806. } else
  3807. wbinvd();
  3808. return X86EMUL_CONTINUE;
  3809. }
  3810. EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
  3811. static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
  3812. {
  3813. kvm_emulate_wbinvd(emul_to_vcpu(ctxt));
  3814. }
  3815. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  3816. {
  3817. return _kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
  3818. }
  3819. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  3820. {
  3821. return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
  3822. }
  3823. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  3824. {
  3825. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  3826. }
  3827. static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
  3828. {
  3829. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3830. unsigned long value;
  3831. switch (cr) {
  3832. case 0:
  3833. value = kvm_read_cr0(vcpu);
  3834. break;
  3835. case 2:
  3836. value = vcpu->arch.cr2;
  3837. break;
  3838. case 3:
  3839. value = kvm_read_cr3(vcpu);
  3840. break;
  3841. case 4:
  3842. value = kvm_read_cr4(vcpu);
  3843. break;
  3844. case 8:
  3845. value = kvm_get_cr8(vcpu);
  3846. break;
  3847. default:
  3848. kvm_err("%s: unexpected cr %u\n", __func__, cr);
  3849. return 0;
  3850. }
  3851. return value;
  3852. }
  3853. static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
  3854. {
  3855. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3856. int res = 0;
  3857. switch (cr) {
  3858. case 0:
  3859. res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
  3860. break;
  3861. case 2:
  3862. vcpu->arch.cr2 = val;
  3863. break;
  3864. case 3:
  3865. res = kvm_set_cr3(vcpu, val);
  3866. break;
  3867. case 4:
  3868. res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
  3869. break;
  3870. case 8:
  3871. res = kvm_set_cr8(vcpu, val);
  3872. break;
  3873. default:
  3874. kvm_err("%s: unexpected cr %u\n", __func__, cr);
  3875. res = -1;
  3876. }
  3877. return res;
  3878. }
  3879. static void emulator_set_rflags(struct x86_emulate_ctxt *ctxt, ulong val)
  3880. {
  3881. kvm_set_rflags(emul_to_vcpu(ctxt), val);
  3882. }
  3883. static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
  3884. {
  3885. return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
  3886. }
  3887. static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  3888. {
  3889. kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
  3890. }
  3891. static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  3892. {
  3893. kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
  3894. }
  3895. static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  3896. {
  3897. kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
  3898. }
  3899. static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  3900. {
  3901. kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
  3902. }
  3903. static unsigned long emulator_get_cached_segment_base(
  3904. struct x86_emulate_ctxt *ctxt, int seg)
  3905. {
  3906. return get_segment_base(emul_to_vcpu(ctxt), seg);
  3907. }
  3908. static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
  3909. struct desc_struct *desc, u32 *base3,
  3910. int seg)
  3911. {
  3912. struct kvm_segment var;
  3913. kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
  3914. *selector = var.selector;
  3915. if (var.unusable)
  3916. return false;
  3917. if (var.g)
  3918. var.limit >>= 12;
  3919. set_desc_limit(desc, var.limit);
  3920. set_desc_base(desc, (unsigned long)var.base);
  3921. #ifdef CONFIG_X86_64
  3922. if (base3)
  3923. *base3 = var.base >> 32;
  3924. #endif
  3925. desc->type = var.type;
  3926. desc->s = var.s;
  3927. desc->dpl = var.dpl;
  3928. desc->p = var.present;
  3929. desc->avl = var.avl;
  3930. desc->l = var.l;
  3931. desc->d = var.db;
  3932. desc->g = var.g;
  3933. return true;
  3934. }
  3935. static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
  3936. struct desc_struct *desc, u32 base3,
  3937. int seg)
  3938. {
  3939. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3940. struct kvm_segment var;
  3941. var.selector = selector;
  3942. var.base = get_desc_base(desc);
  3943. #ifdef CONFIG_X86_64
  3944. var.base |= ((u64)base3) << 32;
  3945. #endif
  3946. var.limit = get_desc_limit(desc);
  3947. if (desc->g)
  3948. var.limit = (var.limit << 12) | 0xfff;
  3949. var.type = desc->type;
  3950. var.present = desc->p;
  3951. var.dpl = desc->dpl;
  3952. var.db = desc->d;
  3953. var.s = desc->s;
  3954. var.l = desc->l;
  3955. var.g = desc->g;
  3956. var.avl = desc->avl;
  3957. var.present = desc->p;
  3958. var.unusable = !var.present;
  3959. var.padding = 0;
  3960. kvm_set_segment(vcpu, &var, seg);
  3961. return;
  3962. }
  3963. static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
  3964. u32 msr_index, u64 *pdata)
  3965. {
  3966. return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
  3967. }
  3968. static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
  3969. u32 msr_index, u64 data)
  3970. {
  3971. struct msr_data msr;
  3972. msr.data = data;
  3973. msr.index = msr_index;
  3974. msr.host_initiated = false;
  3975. return kvm_set_msr(emul_to_vcpu(ctxt), &msr);
  3976. }
  3977. static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
  3978. u32 pmc, u64 *pdata)
  3979. {
  3980. return kvm_pmu_read_pmc(emul_to_vcpu(ctxt), pmc, pdata);
  3981. }
  3982. static void emulator_halt(struct x86_emulate_ctxt *ctxt)
  3983. {
  3984. emul_to_vcpu(ctxt)->arch.halt_request = 1;
  3985. }
  3986. static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
  3987. {
  3988. preempt_disable();
  3989. kvm_load_guest_fpu(emul_to_vcpu(ctxt));
  3990. /*
  3991. * CR0.TS may reference the host fpu state, not the guest fpu state,
  3992. * so it may be clear at this point.
  3993. */
  3994. clts();
  3995. }
  3996. static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
  3997. {
  3998. preempt_enable();
  3999. }
  4000. static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
  4001. struct x86_instruction_info *info,
  4002. enum x86_intercept_stage stage)
  4003. {
  4004. return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
  4005. }
  4006. static void emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
  4007. u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
  4008. {
  4009. kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx);
  4010. }
  4011. static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
  4012. {
  4013. return kvm_register_read(emul_to_vcpu(ctxt), reg);
  4014. }
  4015. static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
  4016. {
  4017. kvm_register_write(emul_to_vcpu(ctxt), reg, val);
  4018. }
  4019. static const struct x86_emulate_ops emulate_ops = {
  4020. .read_gpr = emulator_read_gpr,
  4021. .write_gpr = emulator_write_gpr,
  4022. .read_std = kvm_read_guest_virt_system,
  4023. .write_std = kvm_write_guest_virt_system,
  4024. .fetch = kvm_fetch_guest_virt,
  4025. .read_emulated = emulator_read_emulated,
  4026. .write_emulated = emulator_write_emulated,
  4027. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  4028. .invlpg = emulator_invlpg,
  4029. .pio_in_emulated = emulator_pio_in_emulated,
  4030. .pio_out_emulated = emulator_pio_out_emulated,
  4031. .get_segment = emulator_get_segment,
  4032. .set_segment = emulator_set_segment,
  4033. .get_cached_segment_base = emulator_get_cached_segment_base,
  4034. .get_gdt = emulator_get_gdt,
  4035. .get_idt = emulator_get_idt,
  4036. .set_gdt = emulator_set_gdt,
  4037. .set_idt = emulator_set_idt,
  4038. .get_cr = emulator_get_cr,
  4039. .set_cr = emulator_set_cr,
  4040. .set_rflags = emulator_set_rflags,
  4041. .cpl = emulator_get_cpl,
  4042. .get_dr = emulator_get_dr,
  4043. .set_dr = emulator_set_dr,
  4044. .set_msr = emulator_set_msr,
  4045. .get_msr = emulator_get_msr,
  4046. .read_pmc = emulator_read_pmc,
  4047. .halt = emulator_halt,
  4048. .wbinvd = emulator_wbinvd,
  4049. .fix_hypercall = emulator_fix_hypercall,
  4050. .get_fpu = emulator_get_fpu,
  4051. .put_fpu = emulator_put_fpu,
  4052. .intercept = emulator_intercept,
  4053. .get_cpuid = emulator_get_cpuid,
  4054. };
  4055. static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
  4056. {
  4057. u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu, mask);
  4058. /*
  4059. * an sti; sti; sequence only disable interrupts for the first
  4060. * instruction. So, if the last instruction, be it emulated or
  4061. * not, left the system with the INT_STI flag enabled, it
  4062. * means that the last instruction is an sti. We should not
  4063. * leave the flag on in this case. The same goes for mov ss
  4064. */
  4065. if (!(int_shadow & mask))
  4066. kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
  4067. }
  4068. static void inject_emulated_exception(struct kvm_vcpu *vcpu)
  4069. {
  4070. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4071. if (ctxt->exception.vector == PF_VECTOR)
  4072. kvm_propagate_fault(vcpu, &ctxt->exception);
  4073. else if (ctxt->exception.error_code_valid)
  4074. kvm_queue_exception_e(vcpu, ctxt->exception.vector,
  4075. ctxt->exception.error_code);
  4076. else
  4077. kvm_queue_exception(vcpu, ctxt->exception.vector);
  4078. }
  4079. static void init_decode_cache(struct x86_emulate_ctxt *ctxt)
  4080. {
  4081. memset(&ctxt->twobyte, 0,
  4082. (void *)&ctxt->_regs - (void *)&ctxt->twobyte);
  4083. ctxt->fetch.start = 0;
  4084. ctxt->fetch.end = 0;
  4085. ctxt->io_read.pos = 0;
  4086. ctxt->io_read.end = 0;
  4087. ctxt->mem_read.pos = 0;
  4088. ctxt->mem_read.end = 0;
  4089. }
  4090. static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
  4091. {
  4092. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4093. int cs_db, cs_l;
  4094. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  4095. ctxt->eflags = kvm_get_rflags(vcpu);
  4096. ctxt->eip = kvm_rip_read(vcpu);
  4097. ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
  4098. (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
  4099. cs_l ? X86EMUL_MODE_PROT64 :
  4100. cs_db ? X86EMUL_MODE_PROT32 :
  4101. X86EMUL_MODE_PROT16;
  4102. ctxt->guest_mode = is_guest_mode(vcpu);
  4103. init_decode_cache(ctxt);
  4104. vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
  4105. }
  4106. int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
  4107. {
  4108. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4109. int ret;
  4110. init_emulate_ctxt(vcpu);
  4111. ctxt->op_bytes = 2;
  4112. ctxt->ad_bytes = 2;
  4113. ctxt->_eip = ctxt->eip + inc_eip;
  4114. ret = emulate_int_real(ctxt, irq);
  4115. if (ret != X86EMUL_CONTINUE)
  4116. return EMULATE_FAIL;
  4117. ctxt->eip = ctxt->_eip;
  4118. kvm_rip_write(vcpu, ctxt->eip);
  4119. kvm_set_rflags(vcpu, ctxt->eflags);
  4120. if (irq == NMI_VECTOR)
  4121. vcpu->arch.nmi_pending = 0;
  4122. else
  4123. vcpu->arch.interrupt.pending = false;
  4124. return EMULATE_DONE;
  4125. }
  4126. EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
  4127. static int handle_emulation_failure(struct kvm_vcpu *vcpu)
  4128. {
  4129. int r = EMULATE_DONE;
  4130. ++vcpu->stat.insn_emulation_fail;
  4131. trace_kvm_emulate_insn_failed(vcpu);
  4132. if (!is_guest_mode(vcpu)) {
  4133. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4134. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4135. vcpu->run->internal.ndata = 0;
  4136. r = EMULATE_FAIL;
  4137. }
  4138. kvm_queue_exception(vcpu, UD_VECTOR);
  4139. return r;
  4140. }
  4141. static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t gva)
  4142. {
  4143. gpa_t gpa;
  4144. pfn_t pfn;
  4145. if (tdp_enabled)
  4146. return false;
  4147. /*
  4148. * if emulation was due to access to shadowed page table
  4149. * and it failed try to unshadow page and re-enter the
  4150. * guest to let CPU execute the instruction.
  4151. */
  4152. if (kvm_mmu_unprotect_page_virt(vcpu, gva))
  4153. return true;
  4154. gpa = kvm_mmu_gva_to_gpa_system(vcpu, gva, NULL);
  4155. if (gpa == UNMAPPED_GVA)
  4156. return true; /* let cpu generate fault */
  4157. /*
  4158. * Do not retry the unhandleable instruction if it faults on the
  4159. * readonly host memory, otherwise it will goto a infinite loop:
  4160. * retry instruction -> write #PF -> emulation fail -> retry
  4161. * instruction -> ...
  4162. */
  4163. pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
  4164. if (!is_error_noslot_pfn(pfn)) {
  4165. kvm_release_pfn_clean(pfn);
  4166. return true;
  4167. }
  4168. return false;
  4169. }
  4170. static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
  4171. unsigned long cr2, int emulation_type)
  4172. {
  4173. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4174. unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
  4175. last_retry_eip = vcpu->arch.last_retry_eip;
  4176. last_retry_addr = vcpu->arch.last_retry_addr;
  4177. /*
  4178. * If the emulation is caused by #PF and it is non-page_table
  4179. * writing instruction, it means the VM-EXIT is caused by shadow
  4180. * page protected, we can zap the shadow page and retry this
  4181. * instruction directly.
  4182. *
  4183. * Note: if the guest uses a non-page-table modifying instruction
  4184. * on the PDE that points to the instruction, then we will unmap
  4185. * the instruction and go to an infinite loop. So, we cache the
  4186. * last retried eip and the last fault address, if we meet the eip
  4187. * and the address again, we can break out of the potential infinite
  4188. * loop.
  4189. */
  4190. vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
  4191. if (!(emulation_type & EMULTYPE_RETRY))
  4192. return false;
  4193. if (x86_page_table_writing_insn(ctxt))
  4194. return false;
  4195. if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
  4196. return false;
  4197. vcpu->arch.last_retry_eip = ctxt->eip;
  4198. vcpu->arch.last_retry_addr = cr2;
  4199. if (!vcpu->arch.mmu.direct_map)
  4200. gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
  4201. kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  4202. return true;
  4203. }
  4204. static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
  4205. static int complete_emulated_pio(struct kvm_vcpu *vcpu);
  4206. int x86_emulate_instruction(struct kvm_vcpu *vcpu,
  4207. unsigned long cr2,
  4208. int emulation_type,
  4209. void *insn,
  4210. int insn_len)
  4211. {
  4212. int r;
  4213. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4214. bool writeback = true;
  4215. kvm_clear_exception_queue(vcpu);
  4216. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  4217. init_emulate_ctxt(vcpu);
  4218. ctxt->interruptibility = 0;
  4219. ctxt->have_exception = false;
  4220. ctxt->perm_ok = false;
  4221. ctxt->only_vendor_specific_insn
  4222. = emulation_type & EMULTYPE_TRAP_UD;
  4223. r = x86_decode_insn(ctxt, insn, insn_len);
  4224. trace_kvm_emulate_insn_start(vcpu);
  4225. ++vcpu->stat.insn_emulation;
  4226. if (r != EMULATION_OK) {
  4227. if (emulation_type & EMULTYPE_TRAP_UD)
  4228. return EMULATE_FAIL;
  4229. if (reexecute_instruction(vcpu, cr2))
  4230. return EMULATE_DONE;
  4231. if (emulation_type & EMULTYPE_SKIP)
  4232. return EMULATE_FAIL;
  4233. return handle_emulation_failure(vcpu);
  4234. }
  4235. }
  4236. if (emulation_type & EMULTYPE_SKIP) {
  4237. kvm_rip_write(vcpu, ctxt->_eip);
  4238. return EMULATE_DONE;
  4239. }
  4240. if (retry_instruction(ctxt, cr2, emulation_type))
  4241. return EMULATE_DONE;
  4242. /* this is needed for vmware backdoor interface to work since it
  4243. changes registers values during IO operation */
  4244. if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
  4245. vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
  4246. emulator_invalidate_register_cache(ctxt);
  4247. }
  4248. restart:
  4249. r = x86_emulate_insn(ctxt);
  4250. if (r == EMULATION_INTERCEPTED)
  4251. return EMULATE_DONE;
  4252. if (r == EMULATION_FAILED) {
  4253. if (reexecute_instruction(vcpu, cr2))
  4254. return EMULATE_DONE;
  4255. return handle_emulation_failure(vcpu);
  4256. }
  4257. if (ctxt->have_exception) {
  4258. inject_emulated_exception(vcpu);
  4259. r = EMULATE_DONE;
  4260. } else if (vcpu->arch.pio.count) {
  4261. if (!vcpu->arch.pio.in)
  4262. vcpu->arch.pio.count = 0;
  4263. else {
  4264. writeback = false;
  4265. vcpu->arch.complete_userspace_io = complete_emulated_pio;
  4266. }
  4267. r = EMULATE_DO_MMIO;
  4268. } else if (vcpu->mmio_needed) {
  4269. if (!vcpu->mmio_is_write)
  4270. writeback = false;
  4271. r = EMULATE_DO_MMIO;
  4272. vcpu->arch.complete_userspace_io = complete_emulated_mmio;
  4273. } else if (r == EMULATION_RESTART)
  4274. goto restart;
  4275. else
  4276. r = EMULATE_DONE;
  4277. if (writeback) {
  4278. toggle_interruptibility(vcpu, ctxt->interruptibility);
  4279. kvm_set_rflags(vcpu, ctxt->eflags);
  4280. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4281. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  4282. kvm_rip_write(vcpu, ctxt->eip);
  4283. } else
  4284. vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
  4285. return r;
  4286. }
  4287. EXPORT_SYMBOL_GPL(x86_emulate_instruction);
  4288. int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
  4289. {
  4290. unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4291. int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
  4292. size, port, &val, 1);
  4293. /* do not return to emulator after return from userspace */
  4294. vcpu->arch.pio.count = 0;
  4295. return ret;
  4296. }
  4297. EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
  4298. static void tsc_bad(void *info)
  4299. {
  4300. __this_cpu_write(cpu_tsc_khz, 0);
  4301. }
  4302. static void tsc_khz_changed(void *data)
  4303. {
  4304. struct cpufreq_freqs *freq = data;
  4305. unsigned long khz = 0;
  4306. if (data)
  4307. khz = freq->new;
  4308. else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  4309. khz = cpufreq_quick_get(raw_smp_processor_id());
  4310. if (!khz)
  4311. khz = tsc_khz;
  4312. __this_cpu_write(cpu_tsc_khz, khz);
  4313. }
  4314. static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  4315. void *data)
  4316. {
  4317. struct cpufreq_freqs *freq = data;
  4318. struct kvm *kvm;
  4319. struct kvm_vcpu *vcpu;
  4320. int i, send_ipi = 0;
  4321. /*
  4322. * We allow guests to temporarily run on slowing clocks,
  4323. * provided we notify them after, or to run on accelerating
  4324. * clocks, provided we notify them before. Thus time never
  4325. * goes backwards.
  4326. *
  4327. * However, we have a problem. We can't atomically update
  4328. * the frequency of a given CPU from this function; it is
  4329. * merely a notifier, which can be called from any CPU.
  4330. * Changing the TSC frequency at arbitrary points in time
  4331. * requires a recomputation of local variables related to
  4332. * the TSC for each VCPU. We must flag these local variables
  4333. * to be updated and be sure the update takes place with the
  4334. * new frequency before any guests proceed.
  4335. *
  4336. * Unfortunately, the combination of hotplug CPU and frequency
  4337. * change creates an intractable locking scenario; the order
  4338. * of when these callouts happen is undefined with respect to
  4339. * CPU hotplug, and they can race with each other. As such,
  4340. * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
  4341. * undefined; you can actually have a CPU frequency change take
  4342. * place in between the computation of X and the setting of the
  4343. * variable. To protect against this problem, all updates of
  4344. * the per_cpu tsc_khz variable are done in an interrupt
  4345. * protected IPI, and all callers wishing to update the value
  4346. * must wait for a synchronous IPI to complete (which is trivial
  4347. * if the caller is on the CPU already). This establishes the
  4348. * necessary total order on variable updates.
  4349. *
  4350. * Note that because a guest time update may take place
  4351. * anytime after the setting of the VCPU's request bit, the
  4352. * correct TSC value must be set before the request. However,
  4353. * to ensure the update actually makes it to any guest which
  4354. * starts running in hardware virtualization between the set
  4355. * and the acquisition of the spinlock, we must also ping the
  4356. * CPU after setting the request bit.
  4357. *
  4358. */
  4359. if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
  4360. return 0;
  4361. if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
  4362. return 0;
  4363. smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
  4364. raw_spin_lock(&kvm_lock);
  4365. list_for_each_entry(kvm, &vm_list, vm_list) {
  4366. kvm_for_each_vcpu(i, vcpu, kvm) {
  4367. if (vcpu->cpu != freq->cpu)
  4368. continue;
  4369. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  4370. if (vcpu->cpu != smp_processor_id())
  4371. send_ipi = 1;
  4372. }
  4373. }
  4374. raw_spin_unlock(&kvm_lock);
  4375. if (freq->old < freq->new && send_ipi) {
  4376. /*
  4377. * We upscale the frequency. Must make the guest
  4378. * doesn't see old kvmclock values while running with
  4379. * the new frequency, otherwise we risk the guest sees
  4380. * time go backwards.
  4381. *
  4382. * In case we update the frequency for another cpu
  4383. * (which might be in guest context) send an interrupt
  4384. * to kick the cpu out of guest context. Next time
  4385. * guest context is entered kvmclock will be updated,
  4386. * so the guest will not see stale values.
  4387. */
  4388. smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
  4389. }
  4390. return 0;
  4391. }
  4392. static struct notifier_block kvmclock_cpufreq_notifier_block = {
  4393. .notifier_call = kvmclock_cpufreq_notifier
  4394. };
  4395. static int kvmclock_cpu_notifier(struct notifier_block *nfb,
  4396. unsigned long action, void *hcpu)
  4397. {
  4398. unsigned int cpu = (unsigned long)hcpu;
  4399. switch (action) {
  4400. case CPU_ONLINE:
  4401. case CPU_DOWN_FAILED:
  4402. smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
  4403. break;
  4404. case CPU_DOWN_PREPARE:
  4405. smp_call_function_single(cpu, tsc_bad, NULL, 1);
  4406. break;
  4407. }
  4408. return NOTIFY_OK;
  4409. }
  4410. static struct notifier_block kvmclock_cpu_notifier_block = {
  4411. .notifier_call = kvmclock_cpu_notifier,
  4412. .priority = -INT_MAX
  4413. };
  4414. static void kvm_timer_init(void)
  4415. {
  4416. int cpu;
  4417. max_tsc_khz = tsc_khz;
  4418. register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
  4419. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  4420. #ifdef CONFIG_CPU_FREQ
  4421. struct cpufreq_policy policy;
  4422. memset(&policy, 0, sizeof(policy));
  4423. cpu = get_cpu();
  4424. cpufreq_get_policy(&policy, cpu);
  4425. if (policy.cpuinfo.max_freq)
  4426. max_tsc_khz = policy.cpuinfo.max_freq;
  4427. put_cpu();
  4428. #endif
  4429. cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
  4430. CPUFREQ_TRANSITION_NOTIFIER);
  4431. }
  4432. pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
  4433. for_each_online_cpu(cpu)
  4434. smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
  4435. }
  4436. static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
  4437. int kvm_is_in_guest(void)
  4438. {
  4439. return __this_cpu_read(current_vcpu) != NULL;
  4440. }
  4441. static int kvm_is_user_mode(void)
  4442. {
  4443. int user_mode = 3;
  4444. if (__this_cpu_read(current_vcpu))
  4445. user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
  4446. return user_mode != 0;
  4447. }
  4448. static unsigned long kvm_get_guest_ip(void)
  4449. {
  4450. unsigned long ip = 0;
  4451. if (__this_cpu_read(current_vcpu))
  4452. ip = kvm_rip_read(__this_cpu_read(current_vcpu));
  4453. return ip;
  4454. }
  4455. static struct perf_guest_info_callbacks kvm_guest_cbs = {
  4456. .is_in_guest = kvm_is_in_guest,
  4457. .is_user_mode = kvm_is_user_mode,
  4458. .get_guest_ip = kvm_get_guest_ip,
  4459. };
  4460. void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
  4461. {
  4462. __this_cpu_write(current_vcpu, vcpu);
  4463. }
  4464. EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
  4465. void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
  4466. {
  4467. __this_cpu_write(current_vcpu, NULL);
  4468. }
  4469. EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
  4470. static void kvm_set_mmio_spte_mask(void)
  4471. {
  4472. u64 mask;
  4473. int maxphyaddr = boot_cpu_data.x86_phys_bits;
  4474. /*
  4475. * Set the reserved bits and the present bit of an paging-structure
  4476. * entry to generate page fault with PFER.RSV = 1.
  4477. */
  4478. mask = ((1ull << (62 - maxphyaddr + 1)) - 1) << maxphyaddr;
  4479. mask |= 1ull;
  4480. #ifdef CONFIG_X86_64
  4481. /*
  4482. * If reserved bit is not supported, clear the present bit to disable
  4483. * mmio page fault.
  4484. */
  4485. if (maxphyaddr == 52)
  4486. mask &= ~1ull;
  4487. #endif
  4488. kvm_mmu_set_mmio_spte_mask(mask);
  4489. }
  4490. #ifdef CONFIG_X86_64
  4491. static void pvclock_gtod_update_fn(struct work_struct *work)
  4492. {
  4493. struct kvm *kvm;
  4494. struct kvm_vcpu *vcpu;
  4495. int i;
  4496. raw_spin_lock(&kvm_lock);
  4497. list_for_each_entry(kvm, &vm_list, vm_list)
  4498. kvm_for_each_vcpu(i, vcpu, kvm)
  4499. set_bit(KVM_REQ_MASTERCLOCK_UPDATE, &vcpu->requests);
  4500. atomic_set(&kvm_guest_has_master_clock, 0);
  4501. raw_spin_unlock(&kvm_lock);
  4502. }
  4503. static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
  4504. /*
  4505. * Notification about pvclock gtod data update.
  4506. */
  4507. static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
  4508. void *priv)
  4509. {
  4510. struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
  4511. struct timekeeper *tk = priv;
  4512. update_pvclock_gtod(tk);
  4513. /* disable master clock if host does not trust, or does not
  4514. * use, TSC clocksource
  4515. */
  4516. if (gtod->clock.vclock_mode != VCLOCK_TSC &&
  4517. atomic_read(&kvm_guest_has_master_clock) != 0)
  4518. queue_work(system_long_wq, &pvclock_gtod_work);
  4519. return 0;
  4520. }
  4521. static struct notifier_block pvclock_gtod_notifier = {
  4522. .notifier_call = pvclock_gtod_notify,
  4523. };
  4524. #endif
  4525. int kvm_arch_init(void *opaque)
  4526. {
  4527. int r;
  4528. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  4529. if (kvm_x86_ops) {
  4530. printk(KERN_ERR "kvm: already loaded the other module\n");
  4531. r = -EEXIST;
  4532. goto out;
  4533. }
  4534. if (!ops->cpu_has_kvm_support()) {
  4535. printk(KERN_ERR "kvm: no hardware support\n");
  4536. r = -EOPNOTSUPP;
  4537. goto out;
  4538. }
  4539. if (ops->disabled_by_bios()) {
  4540. printk(KERN_ERR "kvm: disabled by bios\n");
  4541. r = -EOPNOTSUPP;
  4542. goto out;
  4543. }
  4544. r = -ENOMEM;
  4545. shared_msrs = alloc_percpu(struct kvm_shared_msrs);
  4546. if (!shared_msrs) {
  4547. printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
  4548. goto out;
  4549. }
  4550. r = kvm_mmu_module_init();
  4551. if (r)
  4552. goto out_free_percpu;
  4553. kvm_set_mmio_spte_mask();
  4554. kvm_init_msr_list();
  4555. kvm_x86_ops = ops;
  4556. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  4557. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  4558. kvm_timer_init();
  4559. perf_register_guest_info_callbacks(&kvm_guest_cbs);
  4560. if (cpu_has_xsave)
  4561. host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  4562. kvm_lapic_init();
  4563. #ifdef CONFIG_X86_64
  4564. pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
  4565. #endif
  4566. return 0;
  4567. out_free_percpu:
  4568. free_percpu(shared_msrs);
  4569. out:
  4570. return r;
  4571. }
  4572. void kvm_arch_exit(void)
  4573. {
  4574. perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
  4575. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  4576. cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
  4577. CPUFREQ_TRANSITION_NOTIFIER);
  4578. unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
  4579. #ifdef CONFIG_X86_64
  4580. pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
  4581. #endif
  4582. kvm_x86_ops = NULL;
  4583. kvm_mmu_module_exit();
  4584. free_percpu(shared_msrs);
  4585. }
  4586. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  4587. {
  4588. ++vcpu->stat.halt_exits;
  4589. if (irqchip_in_kernel(vcpu->kvm)) {
  4590. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  4591. return 1;
  4592. } else {
  4593. vcpu->run->exit_reason = KVM_EXIT_HLT;
  4594. return 0;
  4595. }
  4596. }
  4597. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  4598. int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  4599. {
  4600. u64 param, ingpa, outgpa, ret;
  4601. uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
  4602. bool fast, longmode;
  4603. int cs_db, cs_l;
  4604. /*
  4605. * hypercall generates UD from non zero cpl and real mode
  4606. * per HYPER-V spec
  4607. */
  4608. if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
  4609. kvm_queue_exception(vcpu, UD_VECTOR);
  4610. return 0;
  4611. }
  4612. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  4613. longmode = is_long_mode(vcpu) && cs_l == 1;
  4614. if (!longmode) {
  4615. param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
  4616. (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
  4617. ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
  4618. (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
  4619. outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
  4620. (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
  4621. }
  4622. #ifdef CONFIG_X86_64
  4623. else {
  4624. param = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4625. ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4626. outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
  4627. }
  4628. #endif
  4629. code = param & 0xffff;
  4630. fast = (param >> 16) & 0x1;
  4631. rep_cnt = (param >> 32) & 0xfff;
  4632. rep_idx = (param >> 48) & 0xfff;
  4633. trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
  4634. switch (code) {
  4635. case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
  4636. kvm_vcpu_on_spin(vcpu);
  4637. break;
  4638. default:
  4639. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  4640. break;
  4641. }
  4642. ret = res | (((u64)rep_done & 0xfff) << 32);
  4643. if (longmode) {
  4644. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  4645. } else {
  4646. kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
  4647. kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
  4648. }
  4649. return 1;
  4650. }
  4651. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  4652. {
  4653. unsigned long nr, a0, a1, a2, a3, ret;
  4654. int r = 1;
  4655. if (kvm_hv_hypercall_enabled(vcpu->kvm))
  4656. return kvm_hv_hypercall(vcpu);
  4657. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4658. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  4659. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4660. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4661. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  4662. trace_kvm_hypercall(nr, a0, a1, a2, a3);
  4663. if (!is_long_mode(vcpu)) {
  4664. nr &= 0xFFFFFFFF;
  4665. a0 &= 0xFFFFFFFF;
  4666. a1 &= 0xFFFFFFFF;
  4667. a2 &= 0xFFFFFFFF;
  4668. a3 &= 0xFFFFFFFF;
  4669. }
  4670. if (kvm_x86_ops->get_cpl(vcpu) != 0) {
  4671. ret = -KVM_EPERM;
  4672. goto out;
  4673. }
  4674. switch (nr) {
  4675. case KVM_HC_VAPIC_POLL_IRQ:
  4676. ret = 0;
  4677. break;
  4678. default:
  4679. ret = -KVM_ENOSYS;
  4680. break;
  4681. }
  4682. out:
  4683. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  4684. ++vcpu->stat.hypercalls;
  4685. return r;
  4686. }
  4687. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  4688. static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
  4689. {
  4690. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4691. char instruction[3];
  4692. unsigned long rip = kvm_rip_read(vcpu);
  4693. /*
  4694. * Blow out the MMU to ensure that no other VCPU has an active mapping
  4695. * to ensure that the updated hypercall appears atomically across all
  4696. * VCPUs.
  4697. */
  4698. kvm_mmu_zap_all(vcpu->kvm);
  4699. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  4700. return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
  4701. }
  4702. /*
  4703. * Check if userspace requested an interrupt window, and that the
  4704. * interrupt window is open.
  4705. *
  4706. * No need to exit to userspace if we already have an interrupt queued.
  4707. */
  4708. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
  4709. {
  4710. return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
  4711. vcpu->run->request_interrupt_window &&
  4712. kvm_arch_interrupt_allowed(vcpu));
  4713. }
  4714. static void post_kvm_run_save(struct kvm_vcpu *vcpu)
  4715. {
  4716. struct kvm_run *kvm_run = vcpu->run;
  4717. kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  4718. kvm_run->cr8 = kvm_get_cr8(vcpu);
  4719. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  4720. if (irqchip_in_kernel(vcpu->kvm))
  4721. kvm_run->ready_for_interrupt_injection = 1;
  4722. else
  4723. kvm_run->ready_for_interrupt_injection =
  4724. kvm_arch_interrupt_allowed(vcpu) &&
  4725. !kvm_cpu_has_interrupt(vcpu) &&
  4726. !kvm_event_needs_reinjection(vcpu);
  4727. }
  4728. static int vapic_enter(struct kvm_vcpu *vcpu)
  4729. {
  4730. struct kvm_lapic *apic = vcpu->arch.apic;
  4731. struct page *page;
  4732. if (!apic || !apic->vapic_addr)
  4733. return 0;
  4734. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  4735. if (is_error_page(page))
  4736. return -EFAULT;
  4737. vcpu->arch.apic->vapic_page = page;
  4738. return 0;
  4739. }
  4740. static void vapic_exit(struct kvm_vcpu *vcpu)
  4741. {
  4742. struct kvm_lapic *apic = vcpu->arch.apic;
  4743. int idx;
  4744. if (!apic || !apic->vapic_addr)
  4745. return;
  4746. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4747. kvm_release_page_dirty(apic->vapic_page);
  4748. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  4749. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4750. }
  4751. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  4752. {
  4753. int max_irr, tpr;
  4754. if (!kvm_x86_ops->update_cr8_intercept)
  4755. return;
  4756. if (!vcpu->arch.apic)
  4757. return;
  4758. if (!vcpu->arch.apic->vapic_addr)
  4759. max_irr = kvm_lapic_find_highest_irr(vcpu);
  4760. else
  4761. max_irr = -1;
  4762. if (max_irr != -1)
  4763. max_irr >>= 4;
  4764. tpr = kvm_lapic_get_cr8(vcpu);
  4765. kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
  4766. }
  4767. static void inject_pending_event(struct kvm_vcpu *vcpu)
  4768. {
  4769. /* try to reinject previous events if any */
  4770. if (vcpu->arch.exception.pending) {
  4771. trace_kvm_inj_exception(vcpu->arch.exception.nr,
  4772. vcpu->arch.exception.has_error_code,
  4773. vcpu->arch.exception.error_code);
  4774. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  4775. vcpu->arch.exception.has_error_code,
  4776. vcpu->arch.exception.error_code,
  4777. vcpu->arch.exception.reinject);
  4778. return;
  4779. }
  4780. if (vcpu->arch.nmi_injected) {
  4781. kvm_x86_ops->set_nmi(vcpu);
  4782. return;
  4783. }
  4784. if (vcpu->arch.interrupt.pending) {
  4785. kvm_x86_ops->set_irq(vcpu);
  4786. return;
  4787. }
  4788. /* try to inject new event if pending */
  4789. if (vcpu->arch.nmi_pending) {
  4790. if (kvm_x86_ops->nmi_allowed(vcpu)) {
  4791. --vcpu->arch.nmi_pending;
  4792. vcpu->arch.nmi_injected = true;
  4793. kvm_x86_ops->set_nmi(vcpu);
  4794. }
  4795. } else if (kvm_cpu_has_interrupt(vcpu)) {
  4796. if (kvm_x86_ops->interrupt_allowed(vcpu)) {
  4797. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
  4798. false);
  4799. kvm_x86_ops->set_irq(vcpu);
  4800. }
  4801. }
  4802. }
  4803. static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
  4804. {
  4805. if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
  4806. !vcpu->guest_xcr0_loaded) {
  4807. /* kvm_set_xcr() also depends on this */
  4808. xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
  4809. vcpu->guest_xcr0_loaded = 1;
  4810. }
  4811. }
  4812. static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
  4813. {
  4814. if (vcpu->guest_xcr0_loaded) {
  4815. if (vcpu->arch.xcr0 != host_xcr0)
  4816. xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
  4817. vcpu->guest_xcr0_loaded = 0;
  4818. }
  4819. }
  4820. static void process_nmi(struct kvm_vcpu *vcpu)
  4821. {
  4822. unsigned limit = 2;
  4823. /*
  4824. * x86 is limited to one NMI running, and one NMI pending after it.
  4825. * If an NMI is already in progress, limit further NMIs to just one.
  4826. * Otherwise, allow two (and we'll inject the first one immediately).
  4827. */
  4828. if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
  4829. limit = 1;
  4830. vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
  4831. vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
  4832. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4833. }
  4834. static void kvm_gen_update_masterclock(struct kvm *kvm)
  4835. {
  4836. #ifdef CONFIG_X86_64
  4837. int i;
  4838. struct kvm_vcpu *vcpu;
  4839. struct kvm_arch *ka = &kvm->arch;
  4840. spin_lock(&ka->pvclock_gtod_sync_lock);
  4841. kvm_make_mclock_inprogress_request(kvm);
  4842. /* no guest entries from this point */
  4843. pvclock_update_vm_gtod_copy(kvm);
  4844. kvm_for_each_vcpu(i, vcpu, kvm)
  4845. set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
  4846. /* guest entries allowed */
  4847. kvm_for_each_vcpu(i, vcpu, kvm)
  4848. clear_bit(KVM_REQ_MCLOCK_INPROGRESS, &vcpu->requests);
  4849. spin_unlock(&ka->pvclock_gtod_sync_lock);
  4850. #endif
  4851. }
  4852. static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
  4853. {
  4854. int r;
  4855. bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
  4856. vcpu->run->request_interrupt_window;
  4857. bool req_immediate_exit = 0;
  4858. if (vcpu->requests) {
  4859. if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
  4860. kvm_mmu_unload(vcpu);
  4861. if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
  4862. __kvm_migrate_timers(vcpu);
  4863. if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
  4864. kvm_gen_update_masterclock(vcpu->kvm);
  4865. if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
  4866. r = kvm_guest_time_update(vcpu);
  4867. if (unlikely(r))
  4868. goto out;
  4869. }
  4870. if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
  4871. kvm_mmu_sync_roots(vcpu);
  4872. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  4873. kvm_x86_ops->tlb_flush(vcpu);
  4874. if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
  4875. vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
  4876. r = 0;
  4877. goto out;
  4878. }
  4879. if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
  4880. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  4881. r = 0;
  4882. goto out;
  4883. }
  4884. if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
  4885. vcpu->fpu_active = 0;
  4886. kvm_x86_ops->fpu_deactivate(vcpu);
  4887. }
  4888. if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
  4889. /* Page is swapped out. Do synthetic halt */
  4890. vcpu->arch.apf.halted = true;
  4891. r = 1;
  4892. goto out;
  4893. }
  4894. if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
  4895. record_steal_time(vcpu);
  4896. if (kvm_check_request(KVM_REQ_NMI, vcpu))
  4897. process_nmi(vcpu);
  4898. req_immediate_exit =
  4899. kvm_check_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
  4900. if (kvm_check_request(KVM_REQ_PMU, vcpu))
  4901. kvm_handle_pmu_event(vcpu);
  4902. if (kvm_check_request(KVM_REQ_PMI, vcpu))
  4903. kvm_deliver_pmi(vcpu);
  4904. }
  4905. if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
  4906. inject_pending_event(vcpu);
  4907. /* enable NMI/IRQ window open exits if needed */
  4908. if (vcpu->arch.nmi_pending)
  4909. kvm_x86_ops->enable_nmi_window(vcpu);
  4910. else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
  4911. kvm_x86_ops->enable_irq_window(vcpu);
  4912. if (kvm_lapic_enabled(vcpu)) {
  4913. update_cr8_intercept(vcpu);
  4914. kvm_lapic_sync_to_vapic(vcpu);
  4915. }
  4916. }
  4917. r = kvm_mmu_reload(vcpu);
  4918. if (unlikely(r)) {
  4919. goto cancel_injection;
  4920. }
  4921. preempt_disable();
  4922. kvm_x86_ops->prepare_guest_switch(vcpu);
  4923. if (vcpu->fpu_active)
  4924. kvm_load_guest_fpu(vcpu);
  4925. kvm_load_guest_xcr0(vcpu);
  4926. vcpu->mode = IN_GUEST_MODE;
  4927. /* We should set ->mode before check ->requests,
  4928. * see the comment in make_all_cpus_request.
  4929. */
  4930. smp_mb();
  4931. local_irq_disable();
  4932. if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
  4933. || need_resched() || signal_pending(current)) {
  4934. vcpu->mode = OUTSIDE_GUEST_MODE;
  4935. smp_wmb();
  4936. local_irq_enable();
  4937. preempt_enable();
  4938. r = 1;
  4939. goto cancel_injection;
  4940. }
  4941. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  4942. if (req_immediate_exit)
  4943. smp_send_reschedule(vcpu->cpu);
  4944. kvm_guest_enter();
  4945. if (unlikely(vcpu->arch.switch_db_regs)) {
  4946. set_debugreg(0, 7);
  4947. set_debugreg(vcpu->arch.eff_db[0], 0);
  4948. set_debugreg(vcpu->arch.eff_db[1], 1);
  4949. set_debugreg(vcpu->arch.eff_db[2], 2);
  4950. set_debugreg(vcpu->arch.eff_db[3], 3);
  4951. }
  4952. trace_kvm_entry(vcpu->vcpu_id);
  4953. kvm_x86_ops->run(vcpu);
  4954. /*
  4955. * If the guest has used debug registers, at least dr7
  4956. * will be disabled while returning to the host.
  4957. * If we don't have active breakpoints in the host, we don't
  4958. * care about the messed up debug address registers. But if
  4959. * we have some of them active, restore the old state.
  4960. */
  4961. if (hw_breakpoint_active())
  4962. hw_breakpoint_restore();
  4963. vcpu->arch.last_guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu,
  4964. native_read_tsc());
  4965. vcpu->mode = OUTSIDE_GUEST_MODE;
  4966. smp_wmb();
  4967. local_irq_enable();
  4968. ++vcpu->stat.exits;
  4969. /*
  4970. * We must have an instruction between local_irq_enable() and
  4971. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  4972. * the interrupt shadow. The stat.exits increment will do nicely.
  4973. * But we need to prevent reordering, hence this barrier():
  4974. */
  4975. barrier();
  4976. kvm_guest_exit();
  4977. preempt_enable();
  4978. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  4979. /*
  4980. * Profile KVM exit RIPs:
  4981. */
  4982. if (unlikely(prof_on == KVM_PROFILING)) {
  4983. unsigned long rip = kvm_rip_read(vcpu);
  4984. profile_hit(KVM_PROFILING, (void *)rip);
  4985. }
  4986. if (unlikely(vcpu->arch.tsc_always_catchup))
  4987. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  4988. if (vcpu->arch.apic_attention)
  4989. kvm_lapic_sync_from_vapic(vcpu);
  4990. r = kvm_x86_ops->handle_exit(vcpu);
  4991. return r;
  4992. cancel_injection:
  4993. kvm_x86_ops->cancel_injection(vcpu);
  4994. if (unlikely(vcpu->arch.apic_attention))
  4995. kvm_lapic_sync_from_vapic(vcpu);
  4996. out:
  4997. return r;
  4998. }
  4999. static int __vcpu_run(struct kvm_vcpu *vcpu)
  5000. {
  5001. int r;
  5002. struct kvm *kvm = vcpu->kvm;
  5003. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  5004. pr_debug("vcpu %d received sipi with vector # %x\n",
  5005. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  5006. kvm_lapic_reset(vcpu);
  5007. r = kvm_vcpu_reset(vcpu);
  5008. if (r)
  5009. return r;
  5010. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  5011. }
  5012. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  5013. r = vapic_enter(vcpu);
  5014. if (r) {
  5015. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  5016. return r;
  5017. }
  5018. r = 1;
  5019. while (r > 0) {
  5020. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
  5021. !vcpu->arch.apf.halted)
  5022. r = vcpu_enter_guest(vcpu);
  5023. else {
  5024. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  5025. kvm_vcpu_block(vcpu);
  5026. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  5027. if (kvm_check_request(KVM_REQ_UNHALT, vcpu))
  5028. {
  5029. switch(vcpu->arch.mp_state) {
  5030. case KVM_MP_STATE_HALTED:
  5031. vcpu->arch.mp_state =
  5032. KVM_MP_STATE_RUNNABLE;
  5033. case KVM_MP_STATE_RUNNABLE:
  5034. vcpu->arch.apf.halted = false;
  5035. break;
  5036. case KVM_MP_STATE_SIPI_RECEIVED:
  5037. default:
  5038. r = -EINTR;
  5039. break;
  5040. }
  5041. }
  5042. }
  5043. if (r <= 0)
  5044. break;
  5045. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  5046. if (kvm_cpu_has_pending_timer(vcpu))
  5047. kvm_inject_pending_timer_irqs(vcpu);
  5048. if (dm_request_for_irq_injection(vcpu)) {
  5049. r = -EINTR;
  5050. vcpu->run->exit_reason = KVM_EXIT_INTR;
  5051. ++vcpu->stat.request_irq_exits;
  5052. }
  5053. kvm_check_async_pf_completion(vcpu);
  5054. if (signal_pending(current)) {
  5055. r = -EINTR;
  5056. vcpu->run->exit_reason = KVM_EXIT_INTR;
  5057. ++vcpu->stat.signal_exits;
  5058. }
  5059. if (need_resched()) {
  5060. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  5061. kvm_resched(vcpu);
  5062. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  5063. }
  5064. }
  5065. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  5066. vapic_exit(vcpu);
  5067. return r;
  5068. }
  5069. static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
  5070. {
  5071. int r;
  5072. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  5073. r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
  5074. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  5075. if (r != EMULATE_DONE)
  5076. return 0;
  5077. return 1;
  5078. }
  5079. static int complete_emulated_pio(struct kvm_vcpu *vcpu)
  5080. {
  5081. BUG_ON(!vcpu->arch.pio.count);
  5082. return complete_emulated_io(vcpu);
  5083. }
  5084. /*
  5085. * Implements the following, as a state machine:
  5086. *
  5087. * read:
  5088. * for each fragment
  5089. * for each mmio piece in the fragment
  5090. * write gpa, len
  5091. * exit
  5092. * copy data
  5093. * execute insn
  5094. *
  5095. * write:
  5096. * for each fragment
  5097. * for each mmio piece in the fragment
  5098. * write gpa, len
  5099. * copy data
  5100. * exit
  5101. */
  5102. static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
  5103. {
  5104. struct kvm_run *run = vcpu->run;
  5105. struct kvm_mmio_fragment *frag;
  5106. unsigned len;
  5107. BUG_ON(!vcpu->mmio_needed);
  5108. /* Complete previous fragment */
  5109. frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
  5110. len = min(8u, frag->len);
  5111. if (!vcpu->mmio_is_write)
  5112. memcpy(frag->data, run->mmio.data, len);
  5113. if (frag->len <= 8) {
  5114. /* Switch to the next fragment. */
  5115. frag++;
  5116. vcpu->mmio_cur_fragment++;
  5117. } else {
  5118. /* Go forward to the next mmio piece. */
  5119. frag->data += len;
  5120. frag->gpa += len;
  5121. frag->len -= len;
  5122. }
  5123. if (vcpu->mmio_cur_fragment == vcpu->mmio_nr_fragments) {
  5124. vcpu->mmio_needed = 0;
  5125. if (vcpu->mmio_is_write)
  5126. return 1;
  5127. vcpu->mmio_read_completed = 1;
  5128. return complete_emulated_io(vcpu);
  5129. }
  5130. run->exit_reason = KVM_EXIT_MMIO;
  5131. run->mmio.phys_addr = frag->gpa;
  5132. if (vcpu->mmio_is_write)
  5133. memcpy(run->mmio.data, frag->data, min(8u, frag->len));
  5134. run->mmio.len = min(8u, frag->len);
  5135. run->mmio.is_write = vcpu->mmio_is_write;
  5136. vcpu->arch.complete_userspace_io = complete_emulated_mmio;
  5137. return 0;
  5138. }
  5139. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  5140. {
  5141. int r;
  5142. sigset_t sigsaved;
  5143. if (!tsk_used_math(current) && init_fpu(current))
  5144. return -ENOMEM;
  5145. if (vcpu->sigset_active)
  5146. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  5147. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  5148. kvm_vcpu_block(vcpu);
  5149. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  5150. r = -EAGAIN;
  5151. goto out;
  5152. }
  5153. /* re-sync apic's tpr */
  5154. if (!irqchip_in_kernel(vcpu->kvm)) {
  5155. if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
  5156. r = -EINVAL;
  5157. goto out;
  5158. }
  5159. }
  5160. if (unlikely(vcpu->arch.complete_userspace_io)) {
  5161. int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
  5162. vcpu->arch.complete_userspace_io = NULL;
  5163. r = cui(vcpu);
  5164. if (r <= 0)
  5165. goto out;
  5166. } else
  5167. WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
  5168. r = __vcpu_run(vcpu);
  5169. out:
  5170. post_kvm_run_save(vcpu);
  5171. if (vcpu->sigset_active)
  5172. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  5173. return r;
  5174. }
  5175. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  5176. {
  5177. if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
  5178. /*
  5179. * We are here if userspace calls get_regs() in the middle of
  5180. * instruction emulation. Registers state needs to be copied
  5181. * back from emulation context to vcpu. Userspace shouldn't do
  5182. * that usually, but some bad designed PV devices (vmware
  5183. * backdoor interface) need this to work
  5184. */
  5185. emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
  5186. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  5187. }
  5188. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  5189. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  5190. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  5191. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  5192. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  5193. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  5194. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  5195. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  5196. #ifdef CONFIG_X86_64
  5197. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  5198. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  5199. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  5200. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  5201. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  5202. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  5203. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  5204. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  5205. #endif
  5206. regs->rip = kvm_rip_read(vcpu);
  5207. regs->rflags = kvm_get_rflags(vcpu);
  5208. return 0;
  5209. }
  5210. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  5211. {
  5212. vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
  5213. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  5214. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  5215. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  5216. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  5217. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  5218. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  5219. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  5220. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  5221. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  5222. #ifdef CONFIG_X86_64
  5223. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  5224. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  5225. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  5226. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  5227. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  5228. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  5229. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  5230. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  5231. #endif
  5232. kvm_rip_write(vcpu, regs->rip);
  5233. kvm_set_rflags(vcpu, regs->rflags);
  5234. vcpu->arch.exception.pending = false;
  5235. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5236. return 0;
  5237. }
  5238. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  5239. {
  5240. struct kvm_segment cs;
  5241. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  5242. *db = cs.db;
  5243. *l = cs.l;
  5244. }
  5245. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  5246. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  5247. struct kvm_sregs *sregs)
  5248. {
  5249. struct desc_ptr dt;
  5250. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  5251. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  5252. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  5253. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  5254. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  5255. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  5256. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  5257. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  5258. kvm_x86_ops->get_idt(vcpu, &dt);
  5259. sregs->idt.limit = dt.size;
  5260. sregs->idt.base = dt.address;
  5261. kvm_x86_ops->get_gdt(vcpu, &dt);
  5262. sregs->gdt.limit = dt.size;
  5263. sregs->gdt.base = dt.address;
  5264. sregs->cr0 = kvm_read_cr0(vcpu);
  5265. sregs->cr2 = vcpu->arch.cr2;
  5266. sregs->cr3 = kvm_read_cr3(vcpu);
  5267. sregs->cr4 = kvm_read_cr4(vcpu);
  5268. sregs->cr8 = kvm_get_cr8(vcpu);
  5269. sregs->efer = vcpu->arch.efer;
  5270. sregs->apic_base = kvm_get_apic_base(vcpu);
  5271. memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
  5272. if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
  5273. set_bit(vcpu->arch.interrupt.nr,
  5274. (unsigned long *)sregs->interrupt_bitmap);
  5275. return 0;
  5276. }
  5277. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  5278. struct kvm_mp_state *mp_state)
  5279. {
  5280. mp_state->mp_state = vcpu->arch.mp_state;
  5281. return 0;
  5282. }
  5283. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  5284. struct kvm_mp_state *mp_state)
  5285. {
  5286. vcpu->arch.mp_state = mp_state->mp_state;
  5287. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5288. return 0;
  5289. }
  5290. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
  5291. int reason, bool has_error_code, u32 error_code)
  5292. {
  5293. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  5294. int ret;
  5295. init_emulate_ctxt(vcpu);
  5296. ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
  5297. has_error_code, error_code);
  5298. if (ret)
  5299. return EMULATE_FAIL;
  5300. kvm_rip_write(vcpu, ctxt->eip);
  5301. kvm_set_rflags(vcpu, ctxt->eflags);
  5302. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5303. return EMULATE_DONE;
  5304. }
  5305. EXPORT_SYMBOL_GPL(kvm_task_switch);
  5306. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  5307. struct kvm_sregs *sregs)
  5308. {
  5309. int mmu_reset_needed = 0;
  5310. int pending_vec, max_bits, idx;
  5311. struct desc_ptr dt;
  5312. if (!guest_cpuid_has_xsave(vcpu) && (sregs->cr4 & X86_CR4_OSXSAVE))
  5313. return -EINVAL;
  5314. dt.size = sregs->idt.limit;
  5315. dt.address = sregs->idt.base;
  5316. kvm_x86_ops->set_idt(vcpu, &dt);
  5317. dt.size = sregs->gdt.limit;
  5318. dt.address = sregs->gdt.base;
  5319. kvm_x86_ops->set_gdt(vcpu, &dt);
  5320. vcpu->arch.cr2 = sregs->cr2;
  5321. mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
  5322. vcpu->arch.cr3 = sregs->cr3;
  5323. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  5324. kvm_set_cr8(vcpu, sregs->cr8);
  5325. mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
  5326. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  5327. kvm_set_apic_base(vcpu, sregs->apic_base);
  5328. mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
  5329. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  5330. vcpu->arch.cr0 = sregs->cr0;
  5331. mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
  5332. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  5333. if (sregs->cr4 & X86_CR4_OSXSAVE)
  5334. kvm_update_cpuid(vcpu);
  5335. idx = srcu_read_lock(&vcpu->kvm->srcu);
  5336. if (!is_long_mode(vcpu) && is_pae(vcpu)) {
  5337. load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
  5338. mmu_reset_needed = 1;
  5339. }
  5340. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  5341. if (mmu_reset_needed)
  5342. kvm_mmu_reset_context(vcpu);
  5343. max_bits = KVM_NR_INTERRUPTS;
  5344. pending_vec = find_first_bit(
  5345. (const unsigned long *)sregs->interrupt_bitmap, max_bits);
  5346. if (pending_vec < max_bits) {
  5347. kvm_queue_interrupt(vcpu, pending_vec, false);
  5348. pr_debug("Set back pending irq %d\n", pending_vec);
  5349. }
  5350. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  5351. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  5352. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  5353. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  5354. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  5355. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  5356. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  5357. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  5358. update_cr8_intercept(vcpu);
  5359. /* Older userspace won't unhalt the vcpu on reset. */
  5360. if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
  5361. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  5362. !is_protmode(vcpu))
  5363. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  5364. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5365. return 0;
  5366. }
  5367. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  5368. struct kvm_guest_debug *dbg)
  5369. {
  5370. unsigned long rflags;
  5371. int i, r;
  5372. if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
  5373. r = -EBUSY;
  5374. if (vcpu->arch.exception.pending)
  5375. goto out;
  5376. if (dbg->control & KVM_GUESTDBG_INJECT_DB)
  5377. kvm_queue_exception(vcpu, DB_VECTOR);
  5378. else
  5379. kvm_queue_exception(vcpu, BP_VECTOR);
  5380. }
  5381. /*
  5382. * Read rflags as long as potentially injected trace flags are still
  5383. * filtered out.
  5384. */
  5385. rflags = kvm_get_rflags(vcpu);
  5386. vcpu->guest_debug = dbg->control;
  5387. if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
  5388. vcpu->guest_debug = 0;
  5389. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  5390. for (i = 0; i < KVM_NR_DB_REGS; ++i)
  5391. vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
  5392. vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
  5393. } else {
  5394. for (i = 0; i < KVM_NR_DB_REGS; i++)
  5395. vcpu->arch.eff_db[i] = vcpu->arch.db[i];
  5396. }
  5397. kvm_update_dr7(vcpu);
  5398. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  5399. vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
  5400. get_segment_base(vcpu, VCPU_SREG_CS);
  5401. /*
  5402. * Trigger an rflags update that will inject or remove the trace
  5403. * flags.
  5404. */
  5405. kvm_set_rflags(vcpu, rflags);
  5406. kvm_x86_ops->update_db_bp_intercept(vcpu);
  5407. r = 0;
  5408. out:
  5409. return r;
  5410. }
  5411. /*
  5412. * Translate a guest virtual address to a guest physical address.
  5413. */
  5414. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  5415. struct kvm_translation *tr)
  5416. {
  5417. unsigned long vaddr = tr->linear_address;
  5418. gpa_t gpa;
  5419. int idx;
  5420. idx = srcu_read_lock(&vcpu->kvm->srcu);
  5421. gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
  5422. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  5423. tr->physical_address = gpa;
  5424. tr->valid = gpa != UNMAPPED_GVA;
  5425. tr->writeable = 1;
  5426. tr->usermode = 0;
  5427. return 0;
  5428. }
  5429. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  5430. {
  5431. struct i387_fxsave_struct *fxsave =
  5432. &vcpu->arch.guest_fpu.state->fxsave;
  5433. memcpy(fpu->fpr, fxsave->st_space, 128);
  5434. fpu->fcw = fxsave->cwd;
  5435. fpu->fsw = fxsave->swd;
  5436. fpu->ftwx = fxsave->twd;
  5437. fpu->last_opcode = fxsave->fop;
  5438. fpu->last_ip = fxsave->rip;
  5439. fpu->last_dp = fxsave->rdp;
  5440. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  5441. return 0;
  5442. }
  5443. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  5444. {
  5445. struct i387_fxsave_struct *fxsave =
  5446. &vcpu->arch.guest_fpu.state->fxsave;
  5447. memcpy(fxsave->st_space, fpu->fpr, 128);
  5448. fxsave->cwd = fpu->fcw;
  5449. fxsave->swd = fpu->fsw;
  5450. fxsave->twd = fpu->ftwx;
  5451. fxsave->fop = fpu->last_opcode;
  5452. fxsave->rip = fpu->last_ip;
  5453. fxsave->rdp = fpu->last_dp;
  5454. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  5455. return 0;
  5456. }
  5457. int fx_init(struct kvm_vcpu *vcpu)
  5458. {
  5459. int err;
  5460. err = fpu_alloc(&vcpu->arch.guest_fpu);
  5461. if (err)
  5462. return err;
  5463. fpu_finit(&vcpu->arch.guest_fpu);
  5464. /*
  5465. * Ensure guest xcr0 is valid for loading
  5466. */
  5467. vcpu->arch.xcr0 = XSTATE_FP;
  5468. vcpu->arch.cr0 |= X86_CR0_ET;
  5469. return 0;
  5470. }
  5471. EXPORT_SYMBOL_GPL(fx_init);
  5472. static void fx_free(struct kvm_vcpu *vcpu)
  5473. {
  5474. fpu_free(&vcpu->arch.guest_fpu);
  5475. }
  5476. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  5477. {
  5478. if (vcpu->guest_fpu_loaded)
  5479. return;
  5480. /*
  5481. * Restore all possible states in the guest,
  5482. * and assume host would use all available bits.
  5483. * Guest xcr0 would be loaded later.
  5484. */
  5485. kvm_put_guest_xcr0(vcpu);
  5486. vcpu->guest_fpu_loaded = 1;
  5487. __kernel_fpu_begin();
  5488. fpu_restore_checking(&vcpu->arch.guest_fpu);
  5489. trace_kvm_fpu(1);
  5490. }
  5491. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  5492. {
  5493. kvm_put_guest_xcr0(vcpu);
  5494. if (!vcpu->guest_fpu_loaded)
  5495. return;
  5496. vcpu->guest_fpu_loaded = 0;
  5497. fpu_save_init(&vcpu->arch.guest_fpu);
  5498. __kernel_fpu_end();
  5499. ++vcpu->stat.fpu_reload;
  5500. kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
  5501. trace_kvm_fpu(0);
  5502. }
  5503. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  5504. {
  5505. kvmclock_reset(vcpu);
  5506. free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
  5507. fx_free(vcpu);
  5508. kvm_x86_ops->vcpu_free(vcpu);
  5509. }
  5510. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  5511. unsigned int id)
  5512. {
  5513. if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
  5514. printk_once(KERN_WARNING
  5515. "kvm: SMP vm created on host with unstable TSC; "
  5516. "guest TSC will not be reliable\n");
  5517. return kvm_x86_ops->vcpu_create(kvm, id);
  5518. }
  5519. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  5520. {
  5521. int r;
  5522. vcpu->arch.mtrr_state.have_fixed = 1;
  5523. r = vcpu_load(vcpu);
  5524. if (r)
  5525. return r;
  5526. r = kvm_vcpu_reset(vcpu);
  5527. if (r == 0)
  5528. r = kvm_mmu_setup(vcpu);
  5529. vcpu_put(vcpu);
  5530. return r;
  5531. }
  5532. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  5533. {
  5534. int r;
  5535. struct msr_data msr;
  5536. r = vcpu_load(vcpu);
  5537. if (r)
  5538. return r;
  5539. msr.data = 0x0;
  5540. msr.index = MSR_IA32_TSC;
  5541. msr.host_initiated = true;
  5542. kvm_write_tsc(vcpu, &msr);
  5543. vcpu_put(vcpu);
  5544. return r;
  5545. }
  5546. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  5547. {
  5548. int r;
  5549. vcpu->arch.apf.msr_val = 0;
  5550. r = vcpu_load(vcpu);
  5551. BUG_ON(r);
  5552. kvm_mmu_unload(vcpu);
  5553. vcpu_put(vcpu);
  5554. fx_free(vcpu);
  5555. kvm_x86_ops->vcpu_free(vcpu);
  5556. }
  5557. static int kvm_vcpu_reset(struct kvm_vcpu *vcpu)
  5558. {
  5559. atomic_set(&vcpu->arch.nmi_queued, 0);
  5560. vcpu->arch.nmi_pending = 0;
  5561. vcpu->arch.nmi_injected = false;
  5562. memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
  5563. vcpu->arch.dr6 = DR6_FIXED_1;
  5564. vcpu->arch.dr7 = DR7_FIXED_1;
  5565. kvm_update_dr7(vcpu);
  5566. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5567. vcpu->arch.apf.msr_val = 0;
  5568. vcpu->arch.st.msr_val = 0;
  5569. kvmclock_reset(vcpu);
  5570. kvm_clear_async_pf_completion_queue(vcpu);
  5571. kvm_async_pf_hash_reset(vcpu);
  5572. vcpu->arch.apf.halted = false;
  5573. kvm_pmu_reset(vcpu);
  5574. memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
  5575. vcpu->arch.regs_avail = ~0;
  5576. vcpu->arch.regs_dirty = ~0;
  5577. return kvm_x86_ops->vcpu_reset(vcpu);
  5578. }
  5579. int kvm_arch_hardware_enable(void *garbage)
  5580. {
  5581. struct kvm *kvm;
  5582. struct kvm_vcpu *vcpu;
  5583. int i;
  5584. int ret;
  5585. u64 local_tsc;
  5586. u64 max_tsc = 0;
  5587. bool stable, backwards_tsc = false;
  5588. kvm_shared_msr_cpu_online();
  5589. ret = kvm_x86_ops->hardware_enable(garbage);
  5590. if (ret != 0)
  5591. return ret;
  5592. local_tsc = native_read_tsc();
  5593. stable = !check_tsc_unstable();
  5594. list_for_each_entry(kvm, &vm_list, vm_list) {
  5595. kvm_for_each_vcpu(i, vcpu, kvm) {
  5596. if (!stable && vcpu->cpu == smp_processor_id())
  5597. set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
  5598. if (stable && vcpu->arch.last_host_tsc > local_tsc) {
  5599. backwards_tsc = true;
  5600. if (vcpu->arch.last_host_tsc > max_tsc)
  5601. max_tsc = vcpu->arch.last_host_tsc;
  5602. }
  5603. }
  5604. }
  5605. /*
  5606. * Sometimes, even reliable TSCs go backwards. This happens on
  5607. * platforms that reset TSC during suspend or hibernate actions, but
  5608. * maintain synchronization. We must compensate. Fortunately, we can
  5609. * detect that condition here, which happens early in CPU bringup,
  5610. * before any KVM threads can be running. Unfortunately, we can't
  5611. * bring the TSCs fully up to date with real time, as we aren't yet far
  5612. * enough into CPU bringup that we know how much real time has actually
  5613. * elapsed; our helper function, get_kernel_ns() will be using boot
  5614. * variables that haven't been updated yet.
  5615. *
  5616. * So we simply find the maximum observed TSC above, then record the
  5617. * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
  5618. * the adjustment will be applied. Note that we accumulate
  5619. * adjustments, in case multiple suspend cycles happen before some VCPU
  5620. * gets a chance to run again. In the event that no KVM threads get a
  5621. * chance to run, we will miss the entire elapsed period, as we'll have
  5622. * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
  5623. * loose cycle time. This isn't too big a deal, since the loss will be
  5624. * uniform across all VCPUs (not to mention the scenario is extremely
  5625. * unlikely). It is possible that a second hibernate recovery happens
  5626. * much faster than a first, causing the observed TSC here to be
  5627. * smaller; this would require additional padding adjustment, which is
  5628. * why we set last_host_tsc to the local tsc observed here.
  5629. *
  5630. * N.B. - this code below runs only on platforms with reliable TSC,
  5631. * as that is the only way backwards_tsc is set above. Also note
  5632. * that this runs for ALL vcpus, which is not a bug; all VCPUs should
  5633. * have the same delta_cyc adjustment applied if backwards_tsc
  5634. * is detected. Note further, this adjustment is only done once,
  5635. * as we reset last_host_tsc on all VCPUs to stop this from being
  5636. * called multiple times (one for each physical CPU bringup).
  5637. *
  5638. * Platforms with unreliable TSCs don't have to deal with this, they
  5639. * will be compensated by the logic in vcpu_load, which sets the TSC to
  5640. * catchup mode. This will catchup all VCPUs to real time, but cannot
  5641. * guarantee that they stay in perfect synchronization.
  5642. */
  5643. if (backwards_tsc) {
  5644. u64 delta_cyc = max_tsc - local_tsc;
  5645. list_for_each_entry(kvm, &vm_list, vm_list) {
  5646. kvm_for_each_vcpu(i, vcpu, kvm) {
  5647. vcpu->arch.tsc_offset_adjustment += delta_cyc;
  5648. vcpu->arch.last_host_tsc = local_tsc;
  5649. set_bit(KVM_REQ_MASTERCLOCK_UPDATE,
  5650. &vcpu->requests);
  5651. }
  5652. /*
  5653. * We have to disable TSC offset matching.. if you were
  5654. * booting a VM while issuing an S4 host suspend....
  5655. * you may have some problem. Solving this issue is
  5656. * left as an exercise to the reader.
  5657. */
  5658. kvm->arch.last_tsc_nsec = 0;
  5659. kvm->arch.last_tsc_write = 0;
  5660. }
  5661. }
  5662. return 0;
  5663. }
  5664. void kvm_arch_hardware_disable(void *garbage)
  5665. {
  5666. kvm_x86_ops->hardware_disable(garbage);
  5667. drop_user_return_notifiers(garbage);
  5668. }
  5669. int kvm_arch_hardware_setup(void)
  5670. {
  5671. return kvm_x86_ops->hardware_setup();
  5672. }
  5673. void kvm_arch_hardware_unsetup(void)
  5674. {
  5675. kvm_x86_ops->hardware_unsetup();
  5676. }
  5677. void kvm_arch_check_processor_compat(void *rtn)
  5678. {
  5679. kvm_x86_ops->check_processor_compatibility(rtn);
  5680. }
  5681. bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
  5682. {
  5683. return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL);
  5684. }
  5685. struct static_key kvm_no_apic_vcpu __read_mostly;
  5686. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  5687. {
  5688. struct page *page;
  5689. struct kvm *kvm;
  5690. int r;
  5691. BUG_ON(vcpu->kvm == NULL);
  5692. kvm = vcpu->kvm;
  5693. vcpu->arch.emulate_ctxt.ops = &emulate_ops;
  5694. if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
  5695. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  5696. else
  5697. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  5698. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  5699. if (!page) {
  5700. r = -ENOMEM;
  5701. goto fail;
  5702. }
  5703. vcpu->arch.pio_data = page_address(page);
  5704. kvm_set_tsc_khz(vcpu, max_tsc_khz);
  5705. r = kvm_mmu_create(vcpu);
  5706. if (r < 0)
  5707. goto fail_free_pio_data;
  5708. if (irqchip_in_kernel(kvm)) {
  5709. r = kvm_create_lapic(vcpu);
  5710. if (r < 0)
  5711. goto fail_mmu_destroy;
  5712. } else
  5713. static_key_slow_inc(&kvm_no_apic_vcpu);
  5714. vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
  5715. GFP_KERNEL);
  5716. if (!vcpu->arch.mce_banks) {
  5717. r = -ENOMEM;
  5718. goto fail_free_lapic;
  5719. }
  5720. vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
  5721. if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL))
  5722. goto fail_free_mce_banks;
  5723. r = fx_init(vcpu);
  5724. if (r)
  5725. goto fail_free_wbinvd_dirty_mask;
  5726. vcpu->arch.ia32_tsc_adjust_msr = 0x0;
  5727. kvm_async_pf_hash_reset(vcpu);
  5728. kvm_pmu_init(vcpu);
  5729. return 0;
  5730. fail_free_wbinvd_dirty_mask:
  5731. free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
  5732. fail_free_mce_banks:
  5733. kfree(vcpu->arch.mce_banks);
  5734. fail_free_lapic:
  5735. kvm_free_lapic(vcpu);
  5736. fail_mmu_destroy:
  5737. kvm_mmu_destroy(vcpu);
  5738. fail_free_pio_data:
  5739. free_page((unsigned long)vcpu->arch.pio_data);
  5740. fail:
  5741. return r;
  5742. }
  5743. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  5744. {
  5745. int idx;
  5746. kvm_pmu_destroy(vcpu);
  5747. kfree(vcpu->arch.mce_banks);
  5748. kvm_free_lapic(vcpu);
  5749. idx = srcu_read_lock(&vcpu->kvm->srcu);
  5750. kvm_mmu_destroy(vcpu);
  5751. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  5752. free_page((unsigned long)vcpu->arch.pio_data);
  5753. if (!irqchip_in_kernel(vcpu->kvm))
  5754. static_key_slow_dec(&kvm_no_apic_vcpu);
  5755. }
  5756. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  5757. {
  5758. if (type)
  5759. return -EINVAL;
  5760. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  5761. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  5762. /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
  5763. set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
  5764. /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
  5765. set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  5766. &kvm->arch.irq_sources_bitmap);
  5767. raw_spin_lock_init(&kvm->arch.tsc_write_lock);
  5768. mutex_init(&kvm->arch.apic_map_lock);
  5769. spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
  5770. pvclock_update_vm_gtod_copy(kvm);
  5771. return 0;
  5772. }
  5773. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  5774. {
  5775. int r;
  5776. r = vcpu_load(vcpu);
  5777. BUG_ON(r);
  5778. kvm_mmu_unload(vcpu);
  5779. vcpu_put(vcpu);
  5780. }
  5781. static void kvm_free_vcpus(struct kvm *kvm)
  5782. {
  5783. unsigned int i;
  5784. struct kvm_vcpu *vcpu;
  5785. /*
  5786. * Unpin any mmu pages first.
  5787. */
  5788. kvm_for_each_vcpu(i, vcpu, kvm) {
  5789. kvm_clear_async_pf_completion_queue(vcpu);
  5790. kvm_unload_vcpu_mmu(vcpu);
  5791. }
  5792. kvm_for_each_vcpu(i, vcpu, kvm)
  5793. kvm_arch_vcpu_free(vcpu);
  5794. mutex_lock(&kvm->lock);
  5795. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  5796. kvm->vcpus[i] = NULL;
  5797. atomic_set(&kvm->online_vcpus, 0);
  5798. mutex_unlock(&kvm->lock);
  5799. }
  5800. void kvm_arch_sync_events(struct kvm *kvm)
  5801. {
  5802. kvm_free_all_assigned_devices(kvm);
  5803. kvm_free_pit(kvm);
  5804. }
  5805. void kvm_arch_destroy_vm(struct kvm *kvm)
  5806. {
  5807. kvm_iommu_unmap_guest(kvm);
  5808. kfree(kvm->arch.vpic);
  5809. kfree(kvm->arch.vioapic);
  5810. kvm_free_vcpus(kvm);
  5811. if (kvm->arch.apic_access_page)
  5812. put_page(kvm->arch.apic_access_page);
  5813. if (kvm->arch.ept_identity_pagetable)
  5814. put_page(kvm->arch.ept_identity_pagetable);
  5815. kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
  5816. }
  5817. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  5818. struct kvm_memory_slot *dont)
  5819. {
  5820. int i;
  5821. for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
  5822. if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
  5823. kvm_kvfree(free->arch.rmap[i]);
  5824. free->arch.rmap[i] = NULL;
  5825. }
  5826. if (i == 0)
  5827. continue;
  5828. if (!dont || free->arch.lpage_info[i - 1] !=
  5829. dont->arch.lpage_info[i - 1]) {
  5830. kvm_kvfree(free->arch.lpage_info[i - 1]);
  5831. free->arch.lpage_info[i - 1] = NULL;
  5832. }
  5833. }
  5834. }
  5835. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  5836. {
  5837. int i;
  5838. for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
  5839. unsigned long ugfn;
  5840. int lpages;
  5841. int level = i + 1;
  5842. lpages = gfn_to_index(slot->base_gfn + npages - 1,
  5843. slot->base_gfn, level) + 1;
  5844. slot->arch.rmap[i] =
  5845. kvm_kvzalloc(lpages * sizeof(*slot->arch.rmap[i]));
  5846. if (!slot->arch.rmap[i])
  5847. goto out_free;
  5848. if (i == 0)
  5849. continue;
  5850. slot->arch.lpage_info[i - 1] = kvm_kvzalloc(lpages *
  5851. sizeof(*slot->arch.lpage_info[i - 1]));
  5852. if (!slot->arch.lpage_info[i - 1])
  5853. goto out_free;
  5854. if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
  5855. slot->arch.lpage_info[i - 1][0].write_count = 1;
  5856. if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
  5857. slot->arch.lpage_info[i - 1][lpages - 1].write_count = 1;
  5858. ugfn = slot->userspace_addr >> PAGE_SHIFT;
  5859. /*
  5860. * If the gfn and userspace address are not aligned wrt each
  5861. * other, or if explicitly asked to, disable large page
  5862. * support for this slot
  5863. */
  5864. if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
  5865. !kvm_largepages_enabled()) {
  5866. unsigned long j;
  5867. for (j = 0; j < lpages; ++j)
  5868. slot->arch.lpage_info[i - 1][j].write_count = 1;
  5869. }
  5870. }
  5871. return 0;
  5872. out_free:
  5873. for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
  5874. kvm_kvfree(slot->arch.rmap[i]);
  5875. slot->arch.rmap[i] = NULL;
  5876. if (i == 0)
  5877. continue;
  5878. kvm_kvfree(slot->arch.lpage_info[i - 1]);
  5879. slot->arch.lpage_info[i - 1] = NULL;
  5880. }
  5881. return -ENOMEM;
  5882. }
  5883. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  5884. struct kvm_memory_slot *memslot,
  5885. struct kvm_memory_slot old,
  5886. struct kvm_userspace_memory_region *mem,
  5887. int user_alloc)
  5888. {
  5889. int npages = memslot->npages;
  5890. int map_flags = MAP_PRIVATE | MAP_ANONYMOUS;
  5891. /* Prevent internal slot pages from being moved by fork()/COW. */
  5892. if (memslot->id >= KVM_MEMORY_SLOTS)
  5893. map_flags = MAP_SHARED | MAP_ANONYMOUS;
  5894. /*To keep backward compatibility with older userspace,
  5895. *x86 needs to handle !user_alloc case.
  5896. */
  5897. if (!user_alloc) {
  5898. if (npages && !old.npages) {
  5899. unsigned long userspace_addr;
  5900. userspace_addr = vm_mmap(NULL, 0,
  5901. npages * PAGE_SIZE,
  5902. PROT_READ | PROT_WRITE,
  5903. map_flags,
  5904. 0);
  5905. if (IS_ERR((void *)userspace_addr))
  5906. return PTR_ERR((void *)userspace_addr);
  5907. memslot->userspace_addr = userspace_addr;
  5908. }
  5909. }
  5910. return 0;
  5911. }
  5912. void kvm_arch_commit_memory_region(struct kvm *kvm,
  5913. struct kvm_userspace_memory_region *mem,
  5914. struct kvm_memory_slot old,
  5915. int user_alloc)
  5916. {
  5917. int nr_mmu_pages = 0, npages = mem->memory_size >> PAGE_SHIFT;
  5918. if (!user_alloc && !old.user_alloc && old.npages && !npages) {
  5919. int ret;
  5920. ret = vm_munmap(old.userspace_addr,
  5921. old.npages * PAGE_SIZE);
  5922. if (ret < 0)
  5923. printk(KERN_WARNING
  5924. "kvm_vm_ioctl_set_memory_region: "
  5925. "failed to munmap memory\n");
  5926. }
  5927. if (!kvm->arch.n_requested_mmu_pages)
  5928. nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  5929. spin_lock(&kvm->mmu_lock);
  5930. if (nr_mmu_pages)
  5931. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  5932. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  5933. spin_unlock(&kvm->mmu_lock);
  5934. /*
  5935. * If memory slot is created, or moved, we need to clear all
  5936. * mmio sptes.
  5937. */
  5938. if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT) {
  5939. kvm_mmu_zap_all(kvm);
  5940. kvm_reload_remote_mmus(kvm);
  5941. }
  5942. }
  5943. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  5944. {
  5945. kvm_mmu_zap_all(kvm);
  5946. kvm_reload_remote_mmus(kvm);
  5947. }
  5948. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  5949. struct kvm_memory_slot *slot)
  5950. {
  5951. kvm_arch_flush_shadow_all(kvm);
  5952. }
  5953. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  5954. {
  5955. return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
  5956. !vcpu->arch.apf.halted)
  5957. || !list_empty_careful(&vcpu->async_pf.done)
  5958. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
  5959. || atomic_read(&vcpu->arch.nmi_queued) ||
  5960. (kvm_arch_interrupt_allowed(vcpu) &&
  5961. kvm_cpu_has_interrupt(vcpu));
  5962. }
  5963. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  5964. {
  5965. return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  5966. }
  5967. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
  5968. {
  5969. return kvm_x86_ops->interrupt_allowed(vcpu);
  5970. }
  5971. bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
  5972. {
  5973. unsigned long current_rip = kvm_rip_read(vcpu) +
  5974. get_segment_base(vcpu, VCPU_SREG_CS);
  5975. return current_rip == linear_rip;
  5976. }
  5977. EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
  5978. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
  5979. {
  5980. unsigned long rflags;
  5981. rflags = kvm_x86_ops->get_rflags(vcpu);
  5982. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  5983. rflags &= ~X86_EFLAGS_TF;
  5984. return rflags;
  5985. }
  5986. EXPORT_SYMBOL_GPL(kvm_get_rflags);
  5987. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  5988. {
  5989. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
  5990. kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
  5991. rflags |= X86_EFLAGS_TF;
  5992. kvm_x86_ops->set_rflags(vcpu, rflags);
  5993. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5994. }
  5995. EXPORT_SYMBOL_GPL(kvm_set_rflags);
  5996. void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
  5997. {
  5998. int r;
  5999. if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
  6000. is_error_page(work->page))
  6001. return;
  6002. r = kvm_mmu_reload(vcpu);
  6003. if (unlikely(r))
  6004. return;
  6005. if (!vcpu->arch.mmu.direct_map &&
  6006. work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
  6007. return;
  6008. vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
  6009. }
  6010. static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
  6011. {
  6012. return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
  6013. }
  6014. static inline u32 kvm_async_pf_next_probe(u32 key)
  6015. {
  6016. return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
  6017. }
  6018. static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  6019. {
  6020. u32 key = kvm_async_pf_hash_fn(gfn);
  6021. while (vcpu->arch.apf.gfns[key] != ~0)
  6022. key = kvm_async_pf_next_probe(key);
  6023. vcpu->arch.apf.gfns[key] = gfn;
  6024. }
  6025. static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
  6026. {
  6027. int i;
  6028. u32 key = kvm_async_pf_hash_fn(gfn);
  6029. for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
  6030. (vcpu->arch.apf.gfns[key] != gfn &&
  6031. vcpu->arch.apf.gfns[key] != ~0); i++)
  6032. key = kvm_async_pf_next_probe(key);
  6033. return key;
  6034. }
  6035. bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  6036. {
  6037. return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
  6038. }
  6039. static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  6040. {
  6041. u32 i, j, k;
  6042. i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
  6043. while (true) {
  6044. vcpu->arch.apf.gfns[i] = ~0;
  6045. do {
  6046. j = kvm_async_pf_next_probe(j);
  6047. if (vcpu->arch.apf.gfns[j] == ~0)
  6048. return;
  6049. k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
  6050. /*
  6051. * k lies cyclically in ]i,j]
  6052. * | i.k.j |
  6053. * |....j i.k.| or |.k..j i...|
  6054. */
  6055. } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
  6056. vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
  6057. i = j;
  6058. }
  6059. }
  6060. static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
  6061. {
  6062. return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
  6063. sizeof(val));
  6064. }
  6065. void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
  6066. struct kvm_async_pf *work)
  6067. {
  6068. struct x86_exception fault;
  6069. trace_kvm_async_pf_not_present(work->arch.token, work->gva);
  6070. kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
  6071. if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
  6072. (vcpu->arch.apf.send_user_only &&
  6073. kvm_x86_ops->get_cpl(vcpu) == 0))
  6074. kvm_make_request(KVM_REQ_APF_HALT, vcpu);
  6075. else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
  6076. fault.vector = PF_VECTOR;
  6077. fault.error_code_valid = true;
  6078. fault.error_code = 0;
  6079. fault.nested_page_fault = false;
  6080. fault.address = work->arch.token;
  6081. kvm_inject_page_fault(vcpu, &fault);
  6082. }
  6083. }
  6084. void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
  6085. struct kvm_async_pf *work)
  6086. {
  6087. struct x86_exception fault;
  6088. trace_kvm_async_pf_ready(work->arch.token, work->gva);
  6089. if (is_error_page(work->page))
  6090. work->arch.token = ~0; /* broadcast wakeup */
  6091. else
  6092. kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
  6093. if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
  6094. !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
  6095. fault.vector = PF_VECTOR;
  6096. fault.error_code_valid = true;
  6097. fault.error_code = 0;
  6098. fault.nested_page_fault = false;
  6099. fault.address = work->arch.token;
  6100. kvm_inject_page_fault(vcpu, &fault);
  6101. }
  6102. vcpu->arch.apf.halted = false;
  6103. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  6104. }
  6105. bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
  6106. {
  6107. if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
  6108. return true;
  6109. else
  6110. return !kvm_event_needs_reinjection(vcpu) &&
  6111. kvm_x86_ops->interrupt_allowed(vcpu);
  6112. }
  6113. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  6114. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
  6115. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
  6116. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
  6117. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
  6118. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
  6119. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
  6120. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
  6121. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
  6122. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
  6123. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
  6124. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);