x86.c 165 KB

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