x86.c 158 KB

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