x86.c 164 KB

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