x86.c 163 KB

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