x86.c 157 KB

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