x86.c 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341
  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. switch (msr) {
  1294. case MSR_EFER:
  1295. return set_efer(vcpu, data);
  1296. case MSR_K7_HWCR:
  1297. data &= ~(u64)0x40; /* ignore flush filter disable */
  1298. data &= ~(u64)0x100; /* ignore ignne emulation enable */
  1299. if (data != 0) {
  1300. pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
  1301. data);
  1302. return 1;
  1303. }
  1304. break;
  1305. case MSR_FAM10H_MMIO_CONF_BASE:
  1306. if (data != 0) {
  1307. pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
  1308. "0x%llx\n", data);
  1309. return 1;
  1310. }
  1311. break;
  1312. case MSR_AMD64_NB_CFG:
  1313. break;
  1314. case MSR_IA32_DEBUGCTLMSR:
  1315. if (!data) {
  1316. /* We support the non-activated case already */
  1317. break;
  1318. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  1319. /* Values other than LBR and BTF are vendor-specific,
  1320. thus reserved and should throw a #GP */
  1321. return 1;
  1322. }
  1323. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  1324. __func__, data);
  1325. break;
  1326. case MSR_IA32_UCODE_REV:
  1327. case MSR_IA32_UCODE_WRITE:
  1328. case MSR_VM_HSAVE_PA:
  1329. case MSR_AMD64_PATCH_LOADER:
  1330. break;
  1331. case 0x200 ... 0x2ff:
  1332. return set_msr_mtrr(vcpu, msr, data);
  1333. case MSR_IA32_APICBASE:
  1334. kvm_set_apic_base(vcpu, data);
  1335. break;
  1336. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1337. return kvm_x2apic_msr_write(vcpu, msr, data);
  1338. case MSR_IA32_TSCDEADLINE:
  1339. kvm_set_lapic_tscdeadline_msr(vcpu, data);
  1340. break;
  1341. case MSR_IA32_MISC_ENABLE:
  1342. vcpu->arch.ia32_misc_enable_msr = data;
  1343. break;
  1344. case MSR_KVM_WALL_CLOCK_NEW:
  1345. case MSR_KVM_WALL_CLOCK:
  1346. vcpu->kvm->arch.wall_clock = data;
  1347. kvm_write_wall_clock(vcpu->kvm, data);
  1348. break;
  1349. case MSR_KVM_SYSTEM_TIME_NEW:
  1350. case MSR_KVM_SYSTEM_TIME: {
  1351. kvmclock_reset(vcpu);
  1352. vcpu->arch.time = data;
  1353. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  1354. /* we verify if the enable bit is set... */
  1355. if (!(data & 1))
  1356. break;
  1357. /* ...but clean it before doing the actual write */
  1358. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  1359. vcpu->arch.time_page =
  1360. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  1361. if (is_error_page(vcpu->arch.time_page)) {
  1362. kvm_release_page_clean(vcpu->arch.time_page);
  1363. vcpu->arch.time_page = NULL;
  1364. }
  1365. break;
  1366. }
  1367. case MSR_KVM_ASYNC_PF_EN:
  1368. if (kvm_pv_enable_async_pf(vcpu, data))
  1369. return 1;
  1370. break;
  1371. case MSR_KVM_STEAL_TIME:
  1372. if (unlikely(!sched_info_on()))
  1373. return 1;
  1374. if (data & KVM_STEAL_RESERVED_MASK)
  1375. return 1;
  1376. if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
  1377. data & KVM_STEAL_VALID_BITS))
  1378. return 1;
  1379. vcpu->arch.st.msr_val = data;
  1380. if (!(data & KVM_MSR_ENABLED))
  1381. break;
  1382. vcpu->arch.st.last_steal = current->sched_info.run_delay;
  1383. preempt_disable();
  1384. accumulate_steal_time(vcpu);
  1385. preempt_enable();
  1386. kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
  1387. break;
  1388. case MSR_IA32_MCG_CTL:
  1389. case MSR_IA32_MCG_STATUS:
  1390. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1391. return set_msr_mce(vcpu, msr, data);
  1392. /* Performance counters are not protected by a CPUID bit,
  1393. * so we should check all of them in the generic path for the sake of
  1394. * cross vendor migration.
  1395. * Writing a zero into the event select MSRs disables them,
  1396. * which we perfectly emulate ;-). Any other value should be at least
  1397. * reported, some guests depend on them.
  1398. */
  1399. case MSR_K7_EVNTSEL0:
  1400. case MSR_K7_EVNTSEL1:
  1401. case MSR_K7_EVNTSEL2:
  1402. case MSR_K7_EVNTSEL3:
  1403. if (data != 0)
  1404. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1405. "0x%x data 0x%llx\n", msr, data);
  1406. break;
  1407. /* at least RHEL 4 unconditionally writes to the perfctr registers,
  1408. * so we ignore writes to make it happy.
  1409. */
  1410. case MSR_K7_PERFCTR0:
  1411. case MSR_K7_PERFCTR1:
  1412. case MSR_K7_PERFCTR2:
  1413. case MSR_K7_PERFCTR3:
  1414. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1415. "0x%x data 0x%llx\n", msr, data);
  1416. break;
  1417. case MSR_K7_CLK_CTL:
  1418. /*
  1419. * Ignore all writes to this no longer documented MSR.
  1420. * Writes are only relevant for old K7 processors,
  1421. * all pre-dating SVM, but a recommended workaround from
  1422. * AMD for these chips. It is possible to speicify the
  1423. * affected processor models on the command line, hence
  1424. * the need to ignore the workaround.
  1425. */
  1426. break;
  1427. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1428. if (kvm_hv_msr_partition_wide(msr)) {
  1429. int r;
  1430. mutex_lock(&vcpu->kvm->lock);
  1431. r = set_msr_hyperv_pw(vcpu, msr, data);
  1432. mutex_unlock(&vcpu->kvm->lock);
  1433. return r;
  1434. } else
  1435. return set_msr_hyperv(vcpu, msr, data);
  1436. break;
  1437. case MSR_IA32_BBL_CR_CTL3:
  1438. /* Drop writes to this legacy MSR -- see rdmsr
  1439. * counterpart for further detail.
  1440. */
  1441. pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", msr, data);
  1442. break;
  1443. default:
  1444. if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
  1445. return xen_hvm_config(vcpu, data);
  1446. if (kvm_pmu_msr(vcpu, msr))
  1447. return kvm_pmu_set_msr(vcpu, msr, data);
  1448. if (!ignore_msrs) {
  1449. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
  1450. msr, data);
  1451. return 1;
  1452. } else {
  1453. pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
  1454. msr, data);
  1455. break;
  1456. }
  1457. }
  1458. return 0;
  1459. }
  1460. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1461. /*
  1462. * Reads an msr value (of 'msr_index') into 'pdata'.
  1463. * Returns 0 on success, non-0 otherwise.
  1464. * Assumes vcpu_load() was already called.
  1465. */
  1466. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1467. {
  1468. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  1469. }
  1470. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1471. {
  1472. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  1473. if (!msr_mtrr_valid(msr))
  1474. return 1;
  1475. if (msr == MSR_MTRRdefType)
  1476. *pdata = vcpu->arch.mtrr_state.def_type +
  1477. (vcpu->arch.mtrr_state.enabled << 10);
  1478. else if (msr == MSR_MTRRfix64K_00000)
  1479. *pdata = p[0];
  1480. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  1481. *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
  1482. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  1483. *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
  1484. else if (msr == MSR_IA32_CR_PAT)
  1485. *pdata = vcpu->arch.pat;
  1486. else { /* Variable MTRRs */
  1487. int idx, is_mtrr_mask;
  1488. u64 *pt;
  1489. idx = (msr - 0x200) / 2;
  1490. is_mtrr_mask = msr - 0x200 - 2 * idx;
  1491. if (!is_mtrr_mask)
  1492. pt =
  1493. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  1494. else
  1495. pt =
  1496. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  1497. *pdata = *pt;
  1498. }
  1499. return 0;
  1500. }
  1501. static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1502. {
  1503. u64 data;
  1504. u64 mcg_cap = vcpu->arch.mcg_cap;
  1505. unsigned bank_num = mcg_cap & 0xff;
  1506. switch (msr) {
  1507. case MSR_IA32_P5_MC_ADDR:
  1508. case MSR_IA32_P5_MC_TYPE:
  1509. data = 0;
  1510. break;
  1511. case MSR_IA32_MCG_CAP:
  1512. data = vcpu->arch.mcg_cap;
  1513. break;
  1514. case MSR_IA32_MCG_CTL:
  1515. if (!(mcg_cap & MCG_CTL_P))
  1516. return 1;
  1517. data = vcpu->arch.mcg_ctl;
  1518. break;
  1519. case MSR_IA32_MCG_STATUS:
  1520. data = vcpu->arch.mcg_status;
  1521. break;
  1522. default:
  1523. if (msr >= MSR_IA32_MC0_CTL &&
  1524. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  1525. u32 offset = msr - MSR_IA32_MC0_CTL;
  1526. data = vcpu->arch.mce_banks[offset];
  1527. break;
  1528. }
  1529. return 1;
  1530. }
  1531. *pdata = data;
  1532. return 0;
  1533. }
  1534. static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1535. {
  1536. u64 data = 0;
  1537. struct kvm *kvm = vcpu->kvm;
  1538. switch (msr) {
  1539. case HV_X64_MSR_GUEST_OS_ID:
  1540. data = kvm->arch.hv_guest_os_id;
  1541. break;
  1542. case HV_X64_MSR_HYPERCALL:
  1543. data = kvm->arch.hv_hypercall;
  1544. break;
  1545. default:
  1546. pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1547. return 1;
  1548. }
  1549. *pdata = data;
  1550. return 0;
  1551. }
  1552. static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1553. {
  1554. u64 data = 0;
  1555. switch (msr) {
  1556. case HV_X64_MSR_VP_INDEX: {
  1557. int r;
  1558. struct kvm_vcpu *v;
  1559. kvm_for_each_vcpu(r, v, vcpu->kvm)
  1560. if (v == vcpu)
  1561. data = r;
  1562. break;
  1563. }
  1564. case HV_X64_MSR_EOI:
  1565. return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
  1566. case HV_X64_MSR_ICR:
  1567. return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
  1568. case HV_X64_MSR_TPR:
  1569. return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
  1570. case HV_X64_MSR_APIC_ASSIST_PAGE:
  1571. data = vcpu->arch.hv_vapic;
  1572. break;
  1573. default:
  1574. pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1575. return 1;
  1576. }
  1577. *pdata = data;
  1578. return 0;
  1579. }
  1580. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1581. {
  1582. u64 data;
  1583. switch (msr) {
  1584. case MSR_IA32_PLATFORM_ID:
  1585. case MSR_IA32_EBL_CR_POWERON:
  1586. case MSR_IA32_DEBUGCTLMSR:
  1587. case MSR_IA32_LASTBRANCHFROMIP:
  1588. case MSR_IA32_LASTBRANCHTOIP:
  1589. case MSR_IA32_LASTINTFROMIP:
  1590. case MSR_IA32_LASTINTTOIP:
  1591. case MSR_K8_SYSCFG:
  1592. case MSR_K7_HWCR:
  1593. case MSR_VM_HSAVE_PA:
  1594. case MSR_K7_EVNTSEL0:
  1595. case MSR_K7_PERFCTR0:
  1596. case MSR_K8_INT_PENDING_MSG:
  1597. case MSR_AMD64_NB_CFG:
  1598. case MSR_FAM10H_MMIO_CONF_BASE:
  1599. data = 0;
  1600. break;
  1601. case MSR_IA32_UCODE_REV:
  1602. data = 0x100000000ULL;
  1603. break;
  1604. case MSR_MTRRcap:
  1605. data = 0x500 | KVM_NR_VAR_MTRR;
  1606. break;
  1607. case 0x200 ... 0x2ff:
  1608. return get_msr_mtrr(vcpu, msr, pdata);
  1609. case 0xcd: /* fsb frequency */
  1610. data = 3;
  1611. break;
  1612. /*
  1613. * MSR_EBC_FREQUENCY_ID
  1614. * Conservative value valid for even the basic CPU models.
  1615. * Models 0,1: 000 in bits 23:21 indicating a bus speed of
  1616. * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
  1617. * and 266MHz for model 3, or 4. Set Core Clock
  1618. * Frequency to System Bus Frequency Ratio to 1 (bits
  1619. * 31:24) even though these are only valid for CPU
  1620. * models > 2, however guests may end up dividing or
  1621. * multiplying by zero otherwise.
  1622. */
  1623. case MSR_EBC_FREQUENCY_ID:
  1624. data = 1 << 24;
  1625. break;
  1626. case MSR_IA32_APICBASE:
  1627. data = kvm_get_apic_base(vcpu);
  1628. break;
  1629. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1630. return kvm_x2apic_msr_read(vcpu, msr, pdata);
  1631. break;
  1632. case MSR_IA32_TSCDEADLINE:
  1633. data = kvm_get_lapic_tscdeadline_msr(vcpu);
  1634. break;
  1635. case MSR_IA32_MISC_ENABLE:
  1636. data = vcpu->arch.ia32_misc_enable_msr;
  1637. break;
  1638. case MSR_IA32_PERF_STATUS:
  1639. /* TSC increment by tick */
  1640. data = 1000ULL;
  1641. /* CPU multiplier */
  1642. data |= (((uint64_t)4ULL) << 40);
  1643. break;
  1644. case MSR_EFER:
  1645. data = vcpu->arch.efer;
  1646. break;
  1647. case MSR_KVM_WALL_CLOCK:
  1648. case MSR_KVM_WALL_CLOCK_NEW:
  1649. data = vcpu->kvm->arch.wall_clock;
  1650. break;
  1651. case MSR_KVM_SYSTEM_TIME:
  1652. case MSR_KVM_SYSTEM_TIME_NEW:
  1653. data = vcpu->arch.time;
  1654. break;
  1655. case MSR_KVM_ASYNC_PF_EN:
  1656. data = vcpu->arch.apf.msr_val;
  1657. break;
  1658. case MSR_KVM_STEAL_TIME:
  1659. data = vcpu->arch.st.msr_val;
  1660. break;
  1661. case MSR_IA32_P5_MC_ADDR:
  1662. case MSR_IA32_P5_MC_TYPE:
  1663. case MSR_IA32_MCG_CAP:
  1664. case MSR_IA32_MCG_CTL:
  1665. case MSR_IA32_MCG_STATUS:
  1666. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1667. return get_msr_mce(vcpu, msr, pdata);
  1668. case MSR_K7_CLK_CTL:
  1669. /*
  1670. * Provide expected ramp-up count for K7. All other
  1671. * are set to zero, indicating minimum divisors for
  1672. * every field.
  1673. *
  1674. * This prevents guest kernels on AMD host with CPU
  1675. * type 6, model 8 and higher from exploding due to
  1676. * the rdmsr failing.
  1677. */
  1678. data = 0x20000000;
  1679. break;
  1680. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1681. if (kvm_hv_msr_partition_wide(msr)) {
  1682. int r;
  1683. mutex_lock(&vcpu->kvm->lock);
  1684. r = get_msr_hyperv_pw(vcpu, msr, pdata);
  1685. mutex_unlock(&vcpu->kvm->lock);
  1686. return r;
  1687. } else
  1688. return get_msr_hyperv(vcpu, msr, pdata);
  1689. break;
  1690. case MSR_IA32_BBL_CR_CTL3:
  1691. /* This legacy MSR exists but isn't fully documented in current
  1692. * silicon. It is however accessed by winxp in very narrow
  1693. * scenarios where it sets bit #19, itself documented as
  1694. * a "reserved" bit. Best effort attempt to source coherent
  1695. * read data here should the balance of the register be
  1696. * interpreted by the guest:
  1697. *
  1698. * L2 cache control register 3: 64GB range, 256KB size,
  1699. * enabled, latency 0x1, configured
  1700. */
  1701. data = 0xbe702111;
  1702. break;
  1703. default:
  1704. if (kvm_pmu_msr(vcpu, msr))
  1705. return kvm_pmu_get_msr(vcpu, msr, pdata);
  1706. if (!ignore_msrs) {
  1707. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  1708. return 1;
  1709. } else {
  1710. pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
  1711. data = 0;
  1712. }
  1713. break;
  1714. }
  1715. *pdata = data;
  1716. return 0;
  1717. }
  1718. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1719. /*
  1720. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1721. *
  1722. * @return number of msrs set successfully.
  1723. */
  1724. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1725. struct kvm_msr_entry *entries,
  1726. int (*do_msr)(struct kvm_vcpu *vcpu,
  1727. unsigned index, u64 *data))
  1728. {
  1729. int i, idx;
  1730. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1731. for (i = 0; i < msrs->nmsrs; ++i)
  1732. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1733. break;
  1734. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1735. return i;
  1736. }
  1737. /*
  1738. * Read or write a bunch of msrs. Parameters are user addresses.
  1739. *
  1740. * @return number of msrs set successfully.
  1741. */
  1742. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1743. int (*do_msr)(struct kvm_vcpu *vcpu,
  1744. unsigned index, u64 *data),
  1745. int writeback)
  1746. {
  1747. struct kvm_msrs msrs;
  1748. struct kvm_msr_entry *entries;
  1749. int r, n;
  1750. unsigned size;
  1751. r = -EFAULT;
  1752. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1753. goto out;
  1754. r = -E2BIG;
  1755. if (msrs.nmsrs >= MAX_IO_MSRS)
  1756. goto out;
  1757. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1758. entries = memdup_user(user_msrs->entries, size);
  1759. if (IS_ERR(entries)) {
  1760. r = PTR_ERR(entries);
  1761. goto out;
  1762. }
  1763. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1764. if (r < 0)
  1765. goto out_free;
  1766. r = -EFAULT;
  1767. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1768. goto out_free;
  1769. r = n;
  1770. out_free:
  1771. kfree(entries);
  1772. out:
  1773. return r;
  1774. }
  1775. int kvm_dev_ioctl_check_extension(long ext)
  1776. {
  1777. int r;
  1778. switch (ext) {
  1779. case KVM_CAP_IRQCHIP:
  1780. case KVM_CAP_HLT:
  1781. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  1782. case KVM_CAP_SET_TSS_ADDR:
  1783. case KVM_CAP_EXT_CPUID:
  1784. case KVM_CAP_CLOCKSOURCE:
  1785. case KVM_CAP_PIT:
  1786. case KVM_CAP_NOP_IO_DELAY:
  1787. case KVM_CAP_MP_STATE:
  1788. case KVM_CAP_SYNC_MMU:
  1789. case KVM_CAP_USER_NMI:
  1790. case KVM_CAP_REINJECT_CONTROL:
  1791. case KVM_CAP_IRQ_INJECT_STATUS:
  1792. case KVM_CAP_ASSIGN_DEV_IRQ:
  1793. case KVM_CAP_IRQFD:
  1794. case KVM_CAP_IOEVENTFD:
  1795. case KVM_CAP_PIT2:
  1796. case KVM_CAP_PIT_STATE2:
  1797. case KVM_CAP_SET_IDENTITY_MAP_ADDR:
  1798. case KVM_CAP_XEN_HVM:
  1799. case KVM_CAP_ADJUST_CLOCK:
  1800. case KVM_CAP_VCPU_EVENTS:
  1801. case KVM_CAP_HYPERV:
  1802. case KVM_CAP_HYPERV_VAPIC:
  1803. case KVM_CAP_HYPERV_SPIN:
  1804. case KVM_CAP_PCI_SEGMENT:
  1805. case KVM_CAP_DEBUGREGS:
  1806. case KVM_CAP_X86_ROBUST_SINGLESTEP:
  1807. case KVM_CAP_XSAVE:
  1808. case KVM_CAP_ASYNC_PF:
  1809. case KVM_CAP_GET_TSC_KHZ:
  1810. r = 1;
  1811. break;
  1812. case KVM_CAP_COALESCED_MMIO:
  1813. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  1814. break;
  1815. case KVM_CAP_VAPIC:
  1816. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  1817. break;
  1818. case KVM_CAP_NR_VCPUS:
  1819. r = KVM_SOFT_MAX_VCPUS;
  1820. break;
  1821. case KVM_CAP_MAX_VCPUS:
  1822. r = KVM_MAX_VCPUS;
  1823. break;
  1824. case KVM_CAP_NR_MEMSLOTS:
  1825. r = KVM_MEMORY_SLOTS;
  1826. break;
  1827. case KVM_CAP_PV_MMU: /* obsolete */
  1828. r = 0;
  1829. break;
  1830. case KVM_CAP_IOMMU:
  1831. r = iommu_present(&pci_bus_type);
  1832. break;
  1833. case KVM_CAP_MCE:
  1834. r = KVM_MAX_MCE_BANKS;
  1835. break;
  1836. case KVM_CAP_XCRS:
  1837. r = cpu_has_xsave;
  1838. break;
  1839. case KVM_CAP_TSC_CONTROL:
  1840. r = kvm_has_tsc_control;
  1841. break;
  1842. case KVM_CAP_TSC_DEADLINE_TIMER:
  1843. r = boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER);
  1844. break;
  1845. default:
  1846. r = 0;
  1847. break;
  1848. }
  1849. return r;
  1850. }
  1851. long kvm_arch_dev_ioctl(struct file *filp,
  1852. unsigned int ioctl, unsigned long arg)
  1853. {
  1854. void __user *argp = (void __user *)arg;
  1855. long r;
  1856. switch (ioctl) {
  1857. case KVM_GET_MSR_INDEX_LIST: {
  1858. struct kvm_msr_list __user *user_msr_list = argp;
  1859. struct kvm_msr_list msr_list;
  1860. unsigned n;
  1861. r = -EFAULT;
  1862. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1863. goto out;
  1864. n = msr_list.nmsrs;
  1865. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1866. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1867. goto out;
  1868. r = -E2BIG;
  1869. if (n < msr_list.nmsrs)
  1870. goto out;
  1871. r = -EFAULT;
  1872. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1873. num_msrs_to_save * sizeof(u32)))
  1874. goto out;
  1875. if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
  1876. &emulated_msrs,
  1877. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1878. goto out;
  1879. r = 0;
  1880. break;
  1881. }
  1882. case KVM_GET_SUPPORTED_CPUID: {
  1883. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1884. struct kvm_cpuid2 cpuid;
  1885. r = -EFAULT;
  1886. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1887. goto out;
  1888. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  1889. cpuid_arg->entries);
  1890. if (r)
  1891. goto out;
  1892. r = -EFAULT;
  1893. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1894. goto out;
  1895. r = 0;
  1896. break;
  1897. }
  1898. case KVM_X86_GET_MCE_CAP_SUPPORTED: {
  1899. u64 mce_cap;
  1900. mce_cap = KVM_MCE_CAP_SUPPORTED;
  1901. r = -EFAULT;
  1902. if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
  1903. goto out;
  1904. r = 0;
  1905. break;
  1906. }
  1907. default:
  1908. r = -EINVAL;
  1909. }
  1910. out:
  1911. return r;
  1912. }
  1913. static void wbinvd_ipi(void *garbage)
  1914. {
  1915. wbinvd();
  1916. }
  1917. static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
  1918. {
  1919. return vcpu->kvm->arch.iommu_domain &&
  1920. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY);
  1921. }
  1922. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1923. {
  1924. /* Address WBINVD may be executed by guest */
  1925. if (need_emulate_wbinvd(vcpu)) {
  1926. if (kvm_x86_ops->has_wbinvd_exit())
  1927. cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
  1928. else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
  1929. smp_call_function_single(vcpu->cpu,
  1930. wbinvd_ipi, NULL, 1);
  1931. }
  1932. kvm_x86_ops->vcpu_load(vcpu, cpu);
  1933. if (unlikely(vcpu->cpu != cpu) || check_tsc_unstable()) {
  1934. /* Make sure TSC doesn't go backwards */
  1935. s64 tsc_delta;
  1936. u64 tsc;
  1937. tsc = kvm_x86_ops->read_l1_tsc(vcpu);
  1938. tsc_delta = !vcpu->arch.last_guest_tsc ? 0 :
  1939. tsc - vcpu->arch.last_guest_tsc;
  1940. if (tsc_delta < 0)
  1941. mark_tsc_unstable("KVM discovered backwards TSC");
  1942. if (check_tsc_unstable()) {
  1943. kvm_x86_ops->adjust_tsc_offset(vcpu, -tsc_delta);
  1944. vcpu->arch.tsc_catchup = 1;
  1945. }
  1946. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  1947. if (vcpu->cpu != cpu)
  1948. kvm_migrate_timers(vcpu);
  1949. vcpu->cpu = cpu;
  1950. }
  1951. accumulate_steal_time(vcpu);
  1952. kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
  1953. }
  1954. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  1955. {
  1956. kvm_x86_ops->vcpu_put(vcpu);
  1957. kvm_put_guest_fpu(vcpu);
  1958. vcpu->arch.last_guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu);
  1959. }
  1960. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  1961. struct kvm_lapic_state *s)
  1962. {
  1963. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  1964. return 0;
  1965. }
  1966. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  1967. struct kvm_lapic_state *s)
  1968. {
  1969. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1970. kvm_apic_post_state_restore(vcpu);
  1971. update_cr8_intercept(vcpu);
  1972. return 0;
  1973. }
  1974. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1975. struct kvm_interrupt *irq)
  1976. {
  1977. if (irq->irq < 0 || irq->irq >= 256)
  1978. return -EINVAL;
  1979. if (irqchip_in_kernel(vcpu->kvm))
  1980. return -ENXIO;
  1981. kvm_queue_interrupt(vcpu, irq->irq, false);
  1982. kvm_make_request(KVM_REQ_EVENT, vcpu);
  1983. return 0;
  1984. }
  1985. static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
  1986. {
  1987. kvm_inject_nmi(vcpu);
  1988. return 0;
  1989. }
  1990. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  1991. struct kvm_tpr_access_ctl *tac)
  1992. {
  1993. if (tac->flags)
  1994. return -EINVAL;
  1995. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  1996. return 0;
  1997. }
  1998. static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
  1999. u64 mcg_cap)
  2000. {
  2001. int r;
  2002. unsigned bank_num = mcg_cap & 0xff, bank;
  2003. r = -EINVAL;
  2004. if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
  2005. goto out;
  2006. if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
  2007. goto out;
  2008. r = 0;
  2009. vcpu->arch.mcg_cap = mcg_cap;
  2010. /* Init IA32_MCG_CTL to all 1s */
  2011. if (mcg_cap & MCG_CTL_P)
  2012. vcpu->arch.mcg_ctl = ~(u64)0;
  2013. /* Init IA32_MCi_CTL to all 1s */
  2014. for (bank = 0; bank < bank_num; bank++)
  2015. vcpu->arch.mce_banks[bank*4] = ~(u64)0;
  2016. out:
  2017. return r;
  2018. }
  2019. static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
  2020. struct kvm_x86_mce *mce)
  2021. {
  2022. u64 mcg_cap = vcpu->arch.mcg_cap;
  2023. unsigned bank_num = mcg_cap & 0xff;
  2024. u64 *banks = vcpu->arch.mce_banks;
  2025. if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
  2026. return -EINVAL;
  2027. /*
  2028. * if IA32_MCG_CTL is not all 1s, the uncorrected error
  2029. * reporting is disabled
  2030. */
  2031. if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
  2032. vcpu->arch.mcg_ctl != ~(u64)0)
  2033. return 0;
  2034. banks += 4 * mce->bank;
  2035. /*
  2036. * if IA32_MCi_CTL is not all 1s, the uncorrected error
  2037. * reporting is disabled for the bank
  2038. */
  2039. if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
  2040. return 0;
  2041. if (mce->status & MCI_STATUS_UC) {
  2042. if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
  2043. !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
  2044. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2045. return 0;
  2046. }
  2047. if (banks[1] & MCI_STATUS_VAL)
  2048. mce->status |= MCI_STATUS_OVER;
  2049. banks[2] = mce->addr;
  2050. banks[3] = mce->misc;
  2051. vcpu->arch.mcg_status = mce->mcg_status;
  2052. banks[1] = mce->status;
  2053. kvm_queue_exception(vcpu, MC_VECTOR);
  2054. } else if (!(banks[1] & MCI_STATUS_VAL)
  2055. || !(banks[1] & MCI_STATUS_UC)) {
  2056. if (banks[1] & MCI_STATUS_VAL)
  2057. mce->status |= MCI_STATUS_OVER;
  2058. banks[2] = mce->addr;
  2059. banks[3] = mce->misc;
  2060. banks[1] = mce->status;
  2061. } else
  2062. banks[1] |= MCI_STATUS_OVER;
  2063. return 0;
  2064. }
  2065. static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
  2066. struct kvm_vcpu_events *events)
  2067. {
  2068. process_nmi(vcpu);
  2069. events->exception.injected =
  2070. vcpu->arch.exception.pending &&
  2071. !kvm_exception_is_soft(vcpu->arch.exception.nr);
  2072. events->exception.nr = vcpu->arch.exception.nr;
  2073. events->exception.has_error_code = vcpu->arch.exception.has_error_code;
  2074. events->exception.pad = 0;
  2075. events->exception.error_code = vcpu->arch.exception.error_code;
  2076. events->interrupt.injected =
  2077. vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
  2078. events->interrupt.nr = vcpu->arch.interrupt.nr;
  2079. events->interrupt.soft = 0;
  2080. events->interrupt.shadow =
  2081. kvm_x86_ops->get_interrupt_shadow(vcpu,
  2082. KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
  2083. events->nmi.injected = vcpu->arch.nmi_injected;
  2084. events->nmi.pending = vcpu->arch.nmi_pending != 0;
  2085. events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
  2086. events->nmi.pad = 0;
  2087. events->sipi_vector = vcpu->arch.sipi_vector;
  2088. events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
  2089. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  2090. | KVM_VCPUEVENT_VALID_SHADOW);
  2091. memset(&events->reserved, 0, sizeof(events->reserved));
  2092. }
  2093. static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
  2094. struct kvm_vcpu_events *events)
  2095. {
  2096. if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
  2097. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  2098. | KVM_VCPUEVENT_VALID_SHADOW))
  2099. return -EINVAL;
  2100. process_nmi(vcpu);
  2101. vcpu->arch.exception.pending = events->exception.injected;
  2102. vcpu->arch.exception.nr = events->exception.nr;
  2103. vcpu->arch.exception.has_error_code = events->exception.has_error_code;
  2104. vcpu->arch.exception.error_code = events->exception.error_code;
  2105. vcpu->arch.interrupt.pending = events->interrupt.injected;
  2106. vcpu->arch.interrupt.nr = events->interrupt.nr;
  2107. vcpu->arch.interrupt.soft = events->interrupt.soft;
  2108. if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
  2109. kvm_x86_ops->set_interrupt_shadow(vcpu,
  2110. events->interrupt.shadow);
  2111. vcpu->arch.nmi_injected = events->nmi.injected;
  2112. if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
  2113. vcpu->arch.nmi_pending = events->nmi.pending;
  2114. kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
  2115. if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
  2116. vcpu->arch.sipi_vector = events->sipi_vector;
  2117. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2118. return 0;
  2119. }
  2120. static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
  2121. struct kvm_debugregs *dbgregs)
  2122. {
  2123. memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
  2124. dbgregs->dr6 = vcpu->arch.dr6;
  2125. dbgregs->dr7 = vcpu->arch.dr7;
  2126. dbgregs->flags = 0;
  2127. memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
  2128. }
  2129. static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
  2130. struct kvm_debugregs *dbgregs)
  2131. {
  2132. if (dbgregs->flags)
  2133. return -EINVAL;
  2134. memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
  2135. vcpu->arch.dr6 = dbgregs->dr6;
  2136. vcpu->arch.dr7 = dbgregs->dr7;
  2137. return 0;
  2138. }
  2139. static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
  2140. struct kvm_xsave *guest_xsave)
  2141. {
  2142. if (cpu_has_xsave)
  2143. memcpy(guest_xsave->region,
  2144. &vcpu->arch.guest_fpu.state->xsave,
  2145. xstate_size);
  2146. else {
  2147. memcpy(guest_xsave->region,
  2148. &vcpu->arch.guest_fpu.state->fxsave,
  2149. sizeof(struct i387_fxsave_struct));
  2150. *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
  2151. XSTATE_FPSSE;
  2152. }
  2153. }
  2154. static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
  2155. struct kvm_xsave *guest_xsave)
  2156. {
  2157. u64 xstate_bv =
  2158. *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
  2159. if (cpu_has_xsave)
  2160. memcpy(&vcpu->arch.guest_fpu.state->xsave,
  2161. guest_xsave->region, xstate_size);
  2162. else {
  2163. if (xstate_bv & ~XSTATE_FPSSE)
  2164. return -EINVAL;
  2165. memcpy(&vcpu->arch.guest_fpu.state->fxsave,
  2166. guest_xsave->region, sizeof(struct i387_fxsave_struct));
  2167. }
  2168. return 0;
  2169. }
  2170. static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
  2171. struct kvm_xcrs *guest_xcrs)
  2172. {
  2173. if (!cpu_has_xsave) {
  2174. guest_xcrs->nr_xcrs = 0;
  2175. return;
  2176. }
  2177. guest_xcrs->nr_xcrs = 1;
  2178. guest_xcrs->flags = 0;
  2179. guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
  2180. guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
  2181. }
  2182. static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
  2183. struct kvm_xcrs *guest_xcrs)
  2184. {
  2185. int i, r = 0;
  2186. if (!cpu_has_xsave)
  2187. return -EINVAL;
  2188. if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
  2189. return -EINVAL;
  2190. for (i = 0; i < guest_xcrs->nr_xcrs; i++)
  2191. /* Only support XCR0 currently */
  2192. if (guest_xcrs->xcrs[0].xcr == XCR_XFEATURE_ENABLED_MASK) {
  2193. r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
  2194. guest_xcrs->xcrs[0].value);
  2195. break;
  2196. }
  2197. if (r)
  2198. r = -EINVAL;
  2199. return r;
  2200. }
  2201. long kvm_arch_vcpu_ioctl(struct file *filp,
  2202. unsigned int ioctl, unsigned long arg)
  2203. {
  2204. struct kvm_vcpu *vcpu = filp->private_data;
  2205. void __user *argp = (void __user *)arg;
  2206. int r;
  2207. union {
  2208. struct kvm_lapic_state *lapic;
  2209. struct kvm_xsave *xsave;
  2210. struct kvm_xcrs *xcrs;
  2211. void *buffer;
  2212. } u;
  2213. u.buffer = NULL;
  2214. switch (ioctl) {
  2215. case KVM_GET_LAPIC: {
  2216. r = -EINVAL;
  2217. if (!vcpu->arch.apic)
  2218. goto out;
  2219. u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  2220. r = -ENOMEM;
  2221. if (!u.lapic)
  2222. goto out;
  2223. r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
  2224. if (r)
  2225. goto out;
  2226. r = -EFAULT;
  2227. if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
  2228. goto out;
  2229. r = 0;
  2230. break;
  2231. }
  2232. case KVM_SET_LAPIC: {
  2233. r = -EINVAL;
  2234. if (!vcpu->arch.apic)
  2235. goto out;
  2236. u.lapic = memdup_user(argp, sizeof(*u.lapic));
  2237. if (IS_ERR(u.lapic)) {
  2238. r = PTR_ERR(u.lapic);
  2239. goto out;
  2240. }
  2241. r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
  2242. if (r)
  2243. goto out;
  2244. r = 0;
  2245. break;
  2246. }
  2247. case KVM_INTERRUPT: {
  2248. struct kvm_interrupt irq;
  2249. r = -EFAULT;
  2250. if (copy_from_user(&irq, argp, sizeof irq))
  2251. goto out;
  2252. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2253. if (r)
  2254. goto out;
  2255. r = 0;
  2256. break;
  2257. }
  2258. case KVM_NMI: {
  2259. r = kvm_vcpu_ioctl_nmi(vcpu);
  2260. if (r)
  2261. goto out;
  2262. r = 0;
  2263. break;
  2264. }
  2265. case KVM_SET_CPUID: {
  2266. struct kvm_cpuid __user *cpuid_arg = argp;
  2267. struct kvm_cpuid cpuid;
  2268. r = -EFAULT;
  2269. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2270. goto out;
  2271. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2272. if (r)
  2273. goto out;
  2274. break;
  2275. }
  2276. case KVM_SET_CPUID2: {
  2277. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2278. struct kvm_cpuid2 cpuid;
  2279. r = -EFAULT;
  2280. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2281. goto out;
  2282. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  2283. cpuid_arg->entries);
  2284. if (r)
  2285. goto out;
  2286. break;
  2287. }
  2288. case KVM_GET_CPUID2: {
  2289. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2290. struct kvm_cpuid2 cpuid;
  2291. r = -EFAULT;
  2292. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2293. goto out;
  2294. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  2295. cpuid_arg->entries);
  2296. if (r)
  2297. goto out;
  2298. r = -EFAULT;
  2299. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  2300. goto out;
  2301. r = 0;
  2302. break;
  2303. }
  2304. case KVM_GET_MSRS:
  2305. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2306. break;
  2307. case KVM_SET_MSRS:
  2308. r = msr_io(vcpu, argp, do_set_msr, 0);
  2309. break;
  2310. case KVM_TPR_ACCESS_REPORTING: {
  2311. struct kvm_tpr_access_ctl tac;
  2312. r = -EFAULT;
  2313. if (copy_from_user(&tac, argp, sizeof tac))
  2314. goto out;
  2315. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  2316. if (r)
  2317. goto out;
  2318. r = -EFAULT;
  2319. if (copy_to_user(argp, &tac, sizeof tac))
  2320. goto out;
  2321. r = 0;
  2322. break;
  2323. };
  2324. case KVM_SET_VAPIC_ADDR: {
  2325. struct kvm_vapic_addr va;
  2326. r = -EINVAL;
  2327. if (!irqchip_in_kernel(vcpu->kvm))
  2328. goto out;
  2329. r = -EFAULT;
  2330. if (copy_from_user(&va, argp, sizeof va))
  2331. goto out;
  2332. r = 0;
  2333. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  2334. break;
  2335. }
  2336. case KVM_X86_SETUP_MCE: {
  2337. u64 mcg_cap;
  2338. r = -EFAULT;
  2339. if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
  2340. goto out;
  2341. r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
  2342. break;
  2343. }
  2344. case KVM_X86_SET_MCE: {
  2345. struct kvm_x86_mce mce;
  2346. r = -EFAULT;
  2347. if (copy_from_user(&mce, argp, sizeof mce))
  2348. goto out;
  2349. r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
  2350. break;
  2351. }
  2352. case KVM_GET_VCPU_EVENTS: {
  2353. struct kvm_vcpu_events events;
  2354. kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
  2355. r = -EFAULT;
  2356. if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
  2357. break;
  2358. r = 0;
  2359. break;
  2360. }
  2361. case KVM_SET_VCPU_EVENTS: {
  2362. struct kvm_vcpu_events events;
  2363. r = -EFAULT;
  2364. if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
  2365. break;
  2366. r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
  2367. break;
  2368. }
  2369. case KVM_GET_DEBUGREGS: {
  2370. struct kvm_debugregs dbgregs;
  2371. kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
  2372. r = -EFAULT;
  2373. if (copy_to_user(argp, &dbgregs,
  2374. sizeof(struct kvm_debugregs)))
  2375. break;
  2376. r = 0;
  2377. break;
  2378. }
  2379. case KVM_SET_DEBUGREGS: {
  2380. struct kvm_debugregs dbgregs;
  2381. r = -EFAULT;
  2382. if (copy_from_user(&dbgregs, argp,
  2383. sizeof(struct kvm_debugregs)))
  2384. break;
  2385. r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
  2386. break;
  2387. }
  2388. case KVM_GET_XSAVE: {
  2389. u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
  2390. r = -ENOMEM;
  2391. if (!u.xsave)
  2392. break;
  2393. kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
  2394. r = -EFAULT;
  2395. if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
  2396. break;
  2397. r = 0;
  2398. break;
  2399. }
  2400. case KVM_SET_XSAVE: {
  2401. u.xsave = memdup_user(argp, sizeof(*u.xsave));
  2402. if (IS_ERR(u.xsave)) {
  2403. r = PTR_ERR(u.xsave);
  2404. goto out;
  2405. }
  2406. r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
  2407. break;
  2408. }
  2409. case KVM_GET_XCRS: {
  2410. u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
  2411. r = -ENOMEM;
  2412. if (!u.xcrs)
  2413. break;
  2414. kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
  2415. r = -EFAULT;
  2416. if (copy_to_user(argp, u.xcrs,
  2417. sizeof(struct kvm_xcrs)))
  2418. break;
  2419. r = 0;
  2420. break;
  2421. }
  2422. case KVM_SET_XCRS: {
  2423. u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
  2424. if (IS_ERR(u.xcrs)) {
  2425. r = PTR_ERR(u.xcrs);
  2426. goto out;
  2427. }
  2428. r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
  2429. break;
  2430. }
  2431. case KVM_SET_TSC_KHZ: {
  2432. u32 user_tsc_khz;
  2433. r = -EINVAL;
  2434. if (!kvm_has_tsc_control)
  2435. break;
  2436. user_tsc_khz = (u32)arg;
  2437. if (user_tsc_khz >= kvm_max_guest_tsc_khz)
  2438. goto out;
  2439. kvm_x86_ops->set_tsc_khz(vcpu, user_tsc_khz);
  2440. r = 0;
  2441. goto out;
  2442. }
  2443. case KVM_GET_TSC_KHZ: {
  2444. r = -EIO;
  2445. if (check_tsc_unstable())
  2446. goto out;
  2447. r = vcpu_tsc_khz(vcpu);
  2448. goto out;
  2449. }
  2450. default:
  2451. r = -EINVAL;
  2452. }
  2453. out:
  2454. kfree(u.buffer);
  2455. return r;
  2456. }
  2457. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  2458. {
  2459. int ret;
  2460. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  2461. return -1;
  2462. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  2463. return ret;
  2464. }
  2465. static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
  2466. u64 ident_addr)
  2467. {
  2468. kvm->arch.ept_identity_map_addr = ident_addr;
  2469. return 0;
  2470. }
  2471. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  2472. u32 kvm_nr_mmu_pages)
  2473. {
  2474. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  2475. return -EINVAL;
  2476. mutex_lock(&kvm->slots_lock);
  2477. spin_lock(&kvm->mmu_lock);
  2478. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  2479. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  2480. spin_unlock(&kvm->mmu_lock);
  2481. mutex_unlock(&kvm->slots_lock);
  2482. return 0;
  2483. }
  2484. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  2485. {
  2486. return kvm->arch.n_max_mmu_pages;
  2487. }
  2488. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2489. {
  2490. int r;
  2491. r = 0;
  2492. switch (chip->chip_id) {
  2493. case KVM_IRQCHIP_PIC_MASTER:
  2494. memcpy(&chip->chip.pic,
  2495. &pic_irqchip(kvm)->pics[0],
  2496. sizeof(struct kvm_pic_state));
  2497. break;
  2498. case KVM_IRQCHIP_PIC_SLAVE:
  2499. memcpy(&chip->chip.pic,
  2500. &pic_irqchip(kvm)->pics[1],
  2501. sizeof(struct kvm_pic_state));
  2502. break;
  2503. case KVM_IRQCHIP_IOAPIC:
  2504. r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
  2505. break;
  2506. default:
  2507. r = -EINVAL;
  2508. break;
  2509. }
  2510. return r;
  2511. }
  2512. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2513. {
  2514. int r;
  2515. r = 0;
  2516. switch (chip->chip_id) {
  2517. case KVM_IRQCHIP_PIC_MASTER:
  2518. spin_lock(&pic_irqchip(kvm)->lock);
  2519. memcpy(&pic_irqchip(kvm)->pics[0],
  2520. &chip->chip.pic,
  2521. sizeof(struct kvm_pic_state));
  2522. spin_unlock(&pic_irqchip(kvm)->lock);
  2523. break;
  2524. case KVM_IRQCHIP_PIC_SLAVE:
  2525. spin_lock(&pic_irqchip(kvm)->lock);
  2526. memcpy(&pic_irqchip(kvm)->pics[1],
  2527. &chip->chip.pic,
  2528. sizeof(struct kvm_pic_state));
  2529. spin_unlock(&pic_irqchip(kvm)->lock);
  2530. break;
  2531. case KVM_IRQCHIP_IOAPIC:
  2532. r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
  2533. break;
  2534. default:
  2535. r = -EINVAL;
  2536. break;
  2537. }
  2538. kvm_pic_update_irq(pic_irqchip(kvm));
  2539. return r;
  2540. }
  2541. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2542. {
  2543. int r = 0;
  2544. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2545. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  2546. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2547. return r;
  2548. }
  2549. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2550. {
  2551. int r = 0;
  2552. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2553. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  2554. kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
  2555. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2556. return r;
  2557. }
  2558. static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2559. {
  2560. int r = 0;
  2561. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2562. memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
  2563. sizeof(ps->channels));
  2564. ps->flags = kvm->arch.vpit->pit_state.flags;
  2565. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2566. memset(&ps->reserved, 0, sizeof(ps->reserved));
  2567. return r;
  2568. }
  2569. static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2570. {
  2571. int r = 0, start = 0;
  2572. u32 prev_legacy, cur_legacy;
  2573. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2574. prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2575. cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2576. if (!prev_legacy && cur_legacy)
  2577. start = 1;
  2578. memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
  2579. sizeof(kvm->arch.vpit->pit_state.channels));
  2580. kvm->arch.vpit->pit_state.flags = ps->flags;
  2581. kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
  2582. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2583. return r;
  2584. }
  2585. static int kvm_vm_ioctl_reinject(struct kvm *kvm,
  2586. struct kvm_reinject_control *control)
  2587. {
  2588. if (!kvm->arch.vpit)
  2589. return -ENXIO;
  2590. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2591. kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
  2592. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2593. return 0;
  2594. }
  2595. /**
  2596. * write_protect_slot - write protect a slot for dirty logging
  2597. * @kvm: the kvm instance
  2598. * @memslot: the slot we protect
  2599. * @dirty_bitmap: the bitmap indicating which pages are dirty
  2600. * @nr_dirty_pages: the number of dirty pages
  2601. *
  2602. * We have two ways to find all sptes to protect:
  2603. * 1. Use kvm_mmu_slot_remove_write_access() which walks all shadow pages and
  2604. * checks ones that have a spte mapping a page in the slot.
  2605. * 2. Use kvm_mmu_rmap_write_protect() for each gfn found in the bitmap.
  2606. *
  2607. * Generally speaking, if there are not so many dirty pages compared to the
  2608. * number of shadow pages, we should use the latter.
  2609. *
  2610. * Note that letting others write into a page marked dirty in the old bitmap
  2611. * by using the remaining tlb entry is not a problem. That page will become
  2612. * write protected again when we flush the tlb and then be reported dirty to
  2613. * the user space by copying the old bitmap.
  2614. */
  2615. static void write_protect_slot(struct kvm *kvm,
  2616. struct kvm_memory_slot *memslot,
  2617. unsigned long *dirty_bitmap,
  2618. unsigned long nr_dirty_pages)
  2619. {
  2620. /* Not many dirty pages compared to # of shadow pages. */
  2621. if (nr_dirty_pages < kvm->arch.n_used_mmu_pages) {
  2622. unsigned long gfn_offset;
  2623. for_each_set_bit(gfn_offset, dirty_bitmap, memslot->npages) {
  2624. unsigned long gfn = memslot->base_gfn + gfn_offset;
  2625. spin_lock(&kvm->mmu_lock);
  2626. kvm_mmu_rmap_write_protect(kvm, gfn, memslot);
  2627. spin_unlock(&kvm->mmu_lock);
  2628. }
  2629. kvm_flush_remote_tlbs(kvm);
  2630. } else {
  2631. spin_lock(&kvm->mmu_lock);
  2632. kvm_mmu_slot_remove_write_access(kvm, memslot->id);
  2633. spin_unlock(&kvm->mmu_lock);
  2634. }
  2635. }
  2636. /*
  2637. * Get (and clear) the dirty memory log for a memory slot.
  2638. */
  2639. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  2640. struct kvm_dirty_log *log)
  2641. {
  2642. int r;
  2643. struct kvm_memory_slot *memslot;
  2644. unsigned long n, nr_dirty_pages;
  2645. mutex_lock(&kvm->slots_lock);
  2646. r = -EINVAL;
  2647. if (log->slot >= KVM_MEMORY_SLOTS)
  2648. goto out;
  2649. memslot = id_to_memslot(kvm->memslots, log->slot);
  2650. r = -ENOENT;
  2651. if (!memslot->dirty_bitmap)
  2652. goto out;
  2653. n = kvm_dirty_bitmap_bytes(memslot);
  2654. nr_dirty_pages = memslot->nr_dirty_pages;
  2655. /* If nothing is dirty, don't bother messing with page tables. */
  2656. if (nr_dirty_pages) {
  2657. struct kvm_memslots *slots, *old_slots;
  2658. unsigned long *dirty_bitmap, *dirty_bitmap_head;
  2659. dirty_bitmap = memslot->dirty_bitmap;
  2660. dirty_bitmap_head = memslot->dirty_bitmap_head;
  2661. if (dirty_bitmap == dirty_bitmap_head)
  2662. dirty_bitmap_head += n / sizeof(long);
  2663. memset(dirty_bitmap_head, 0, n);
  2664. r = -ENOMEM;
  2665. slots = kmemdup(kvm->memslots, sizeof(*kvm->memslots), GFP_KERNEL);
  2666. if (!slots)
  2667. goto out;
  2668. memslot = id_to_memslot(slots, log->slot);
  2669. memslot->nr_dirty_pages = 0;
  2670. memslot->dirty_bitmap = dirty_bitmap_head;
  2671. update_memslots(slots, NULL);
  2672. old_slots = kvm->memslots;
  2673. rcu_assign_pointer(kvm->memslots, slots);
  2674. synchronize_srcu_expedited(&kvm->srcu);
  2675. kfree(old_slots);
  2676. write_protect_slot(kvm, memslot, dirty_bitmap, nr_dirty_pages);
  2677. r = -EFAULT;
  2678. if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
  2679. goto out;
  2680. } else {
  2681. r = -EFAULT;
  2682. if (clear_user(log->dirty_bitmap, n))
  2683. goto out;
  2684. }
  2685. r = 0;
  2686. out:
  2687. mutex_unlock(&kvm->slots_lock);
  2688. return r;
  2689. }
  2690. long kvm_arch_vm_ioctl(struct file *filp,
  2691. unsigned int ioctl, unsigned long arg)
  2692. {
  2693. struct kvm *kvm = filp->private_data;
  2694. void __user *argp = (void __user *)arg;
  2695. int r = -ENOTTY;
  2696. /*
  2697. * This union makes it completely explicit to gcc-3.x
  2698. * that these two variables' stack usage should be
  2699. * combined, not added together.
  2700. */
  2701. union {
  2702. struct kvm_pit_state ps;
  2703. struct kvm_pit_state2 ps2;
  2704. struct kvm_pit_config pit_config;
  2705. } u;
  2706. switch (ioctl) {
  2707. case KVM_SET_TSS_ADDR:
  2708. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  2709. if (r < 0)
  2710. goto out;
  2711. break;
  2712. case KVM_SET_IDENTITY_MAP_ADDR: {
  2713. u64 ident_addr;
  2714. r = -EFAULT;
  2715. if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
  2716. goto out;
  2717. r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
  2718. if (r < 0)
  2719. goto out;
  2720. break;
  2721. }
  2722. case KVM_SET_NR_MMU_PAGES:
  2723. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  2724. if (r)
  2725. goto out;
  2726. break;
  2727. case KVM_GET_NR_MMU_PAGES:
  2728. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  2729. break;
  2730. case KVM_CREATE_IRQCHIP: {
  2731. struct kvm_pic *vpic;
  2732. mutex_lock(&kvm->lock);
  2733. r = -EEXIST;
  2734. if (kvm->arch.vpic)
  2735. goto create_irqchip_unlock;
  2736. r = -ENOMEM;
  2737. vpic = kvm_create_pic(kvm);
  2738. if (vpic) {
  2739. r = kvm_ioapic_init(kvm);
  2740. if (r) {
  2741. mutex_lock(&kvm->slots_lock);
  2742. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  2743. &vpic->dev_master);
  2744. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  2745. &vpic->dev_slave);
  2746. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  2747. &vpic->dev_eclr);
  2748. mutex_unlock(&kvm->slots_lock);
  2749. kfree(vpic);
  2750. goto create_irqchip_unlock;
  2751. }
  2752. } else
  2753. goto create_irqchip_unlock;
  2754. smp_wmb();
  2755. kvm->arch.vpic = vpic;
  2756. smp_wmb();
  2757. r = kvm_setup_default_irq_routing(kvm);
  2758. if (r) {
  2759. mutex_lock(&kvm->slots_lock);
  2760. mutex_lock(&kvm->irq_lock);
  2761. kvm_ioapic_destroy(kvm);
  2762. kvm_destroy_pic(kvm);
  2763. mutex_unlock(&kvm->irq_lock);
  2764. mutex_unlock(&kvm->slots_lock);
  2765. }
  2766. create_irqchip_unlock:
  2767. mutex_unlock(&kvm->lock);
  2768. break;
  2769. }
  2770. case KVM_CREATE_PIT:
  2771. u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
  2772. goto create_pit;
  2773. case KVM_CREATE_PIT2:
  2774. r = -EFAULT;
  2775. if (copy_from_user(&u.pit_config, argp,
  2776. sizeof(struct kvm_pit_config)))
  2777. goto out;
  2778. create_pit:
  2779. mutex_lock(&kvm->slots_lock);
  2780. r = -EEXIST;
  2781. if (kvm->arch.vpit)
  2782. goto create_pit_unlock;
  2783. r = -ENOMEM;
  2784. kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
  2785. if (kvm->arch.vpit)
  2786. r = 0;
  2787. create_pit_unlock:
  2788. mutex_unlock(&kvm->slots_lock);
  2789. break;
  2790. case KVM_IRQ_LINE_STATUS:
  2791. case KVM_IRQ_LINE: {
  2792. struct kvm_irq_level irq_event;
  2793. r = -EFAULT;
  2794. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  2795. goto out;
  2796. r = -ENXIO;
  2797. if (irqchip_in_kernel(kvm)) {
  2798. __s32 status;
  2799. status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  2800. irq_event.irq, irq_event.level);
  2801. if (ioctl == KVM_IRQ_LINE_STATUS) {
  2802. r = -EFAULT;
  2803. irq_event.status = status;
  2804. if (copy_to_user(argp, &irq_event,
  2805. sizeof irq_event))
  2806. goto out;
  2807. }
  2808. r = 0;
  2809. }
  2810. break;
  2811. }
  2812. case KVM_GET_IRQCHIP: {
  2813. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2814. struct kvm_irqchip *chip;
  2815. chip = memdup_user(argp, sizeof(*chip));
  2816. if (IS_ERR(chip)) {
  2817. r = PTR_ERR(chip);
  2818. goto out;
  2819. }
  2820. r = -ENXIO;
  2821. if (!irqchip_in_kernel(kvm))
  2822. goto get_irqchip_out;
  2823. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  2824. if (r)
  2825. goto get_irqchip_out;
  2826. r = -EFAULT;
  2827. if (copy_to_user(argp, chip, sizeof *chip))
  2828. goto get_irqchip_out;
  2829. r = 0;
  2830. get_irqchip_out:
  2831. kfree(chip);
  2832. if (r)
  2833. goto out;
  2834. break;
  2835. }
  2836. case KVM_SET_IRQCHIP: {
  2837. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2838. struct kvm_irqchip *chip;
  2839. chip = memdup_user(argp, sizeof(*chip));
  2840. if (IS_ERR(chip)) {
  2841. r = PTR_ERR(chip);
  2842. goto out;
  2843. }
  2844. r = -ENXIO;
  2845. if (!irqchip_in_kernel(kvm))
  2846. goto set_irqchip_out;
  2847. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  2848. if (r)
  2849. goto set_irqchip_out;
  2850. r = 0;
  2851. set_irqchip_out:
  2852. kfree(chip);
  2853. if (r)
  2854. goto out;
  2855. break;
  2856. }
  2857. case KVM_GET_PIT: {
  2858. r = -EFAULT;
  2859. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  2860. goto out;
  2861. r = -ENXIO;
  2862. if (!kvm->arch.vpit)
  2863. goto out;
  2864. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  2865. if (r)
  2866. goto out;
  2867. r = -EFAULT;
  2868. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  2869. goto out;
  2870. r = 0;
  2871. break;
  2872. }
  2873. case KVM_SET_PIT: {
  2874. r = -EFAULT;
  2875. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  2876. goto out;
  2877. r = -ENXIO;
  2878. if (!kvm->arch.vpit)
  2879. goto out;
  2880. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  2881. if (r)
  2882. goto out;
  2883. r = 0;
  2884. break;
  2885. }
  2886. case KVM_GET_PIT2: {
  2887. r = -ENXIO;
  2888. if (!kvm->arch.vpit)
  2889. goto out;
  2890. r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
  2891. if (r)
  2892. goto out;
  2893. r = -EFAULT;
  2894. if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
  2895. goto out;
  2896. r = 0;
  2897. break;
  2898. }
  2899. case KVM_SET_PIT2: {
  2900. r = -EFAULT;
  2901. if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
  2902. goto out;
  2903. r = -ENXIO;
  2904. if (!kvm->arch.vpit)
  2905. goto out;
  2906. r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
  2907. if (r)
  2908. goto out;
  2909. r = 0;
  2910. break;
  2911. }
  2912. case KVM_REINJECT_CONTROL: {
  2913. struct kvm_reinject_control control;
  2914. r = -EFAULT;
  2915. if (copy_from_user(&control, argp, sizeof(control)))
  2916. goto out;
  2917. r = kvm_vm_ioctl_reinject(kvm, &control);
  2918. if (r)
  2919. goto out;
  2920. r = 0;
  2921. break;
  2922. }
  2923. case KVM_XEN_HVM_CONFIG: {
  2924. r = -EFAULT;
  2925. if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
  2926. sizeof(struct kvm_xen_hvm_config)))
  2927. goto out;
  2928. r = -EINVAL;
  2929. if (kvm->arch.xen_hvm_config.flags)
  2930. goto out;
  2931. r = 0;
  2932. break;
  2933. }
  2934. case KVM_SET_CLOCK: {
  2935. struct kvm_clock_data user_ns;
  2936. u64 now_ns;
  2937. s64 delta;
  2938. r = -EFAULT;
  2939. if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
  2940. goto out;
  2941. r = -EINVAL;
  2942. if (user_ns.flags)
  2943. goto out;
  2944. r = 0;
  2945. local_irq_disable();
  2946. now_ns = get_kernel_ns();
  2947. delta = user_ns.clock - now_ns;
  2948. local_irq_enable();
  2949. kvm->arch.kvmclock_offset = delta;
  2950. break;
  2951. }
  2952. case KVM_GET_CLOCK: {
  2953. struct kvm_clock_data user_ns;
  2954. u64 now_ns;
  2955. local_irq_disable();
  2956. now_ns = get_kernel_ns();
  2957. user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
  2958. local_irq_enable();
  2959. user_ns.flags = 0;
  2960. memset(&user_ns.pad, 0, sizeof(user_ns.pad));
  2961. r = -EFAULT;
  2962. if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
  2963. goto out;
  2964. r = 0;
  2965. break;
  2966. }
  2967. default:
  2968. ;
  2969. }
  2970. out:
  2971. return r;
  2972. }
  2973. static void kvm_init_msr_list(void)
  2974. {
  2975. u32 dummy[2];
  2976. unsigned i, j;
  2977. /* skip the first msrs in the list. KVM-specific */
  2978. for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
  2979. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  2980. continue;
  2981. if (j < i)
  2982. msrs_to_save[j] = msrs_to_save[i];
  2983. j++;
  2984. }
  2985. num_msrs_to_save = j;
  2986. }
  2987. static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
  2988. const void *v)
  2989. {
  2990. int handled = 0;
  2991. int n;
  2992. do {
  2993. n = min(len, 8);
  2994. if (!(vcpu->arch.apic &&
  2995. !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, n, v))
  2996. && kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
  2997. break;
  2998. handled += n;
  2999. addr += n;
  3000. len -= n;
  3001. v += n;
  3002. } while (len);
  3003. return handled;
  3004. }
  3005. static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
  3006. {
  3007. int handled = 0;
  3008. int n;
  3009. do {
  3010. n = min(len, 8);
  3011. if (!(vcpu->arch.apic &&
  3012. !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, n, v))
  3013. && kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
  3014. break;
  3015. trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
  3016. handled += n;
  3017. addr += n;
  3018. len -= n;
  3019. v += n;
  3020. } while (len);
  3021. return handled;
  3022. }
  3023. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  3024. struct kvm_segment *var, int seg)
  3025. {
  3026. kvm_x86_ops->set_segment(vcpu, var, seg);
  3027. }
  3028. void kvm_get_segment(struct kvm_vcpu *vcpu,
  3029. struct kvm_segment *var, int seg)
  3030. {
  3031. kvm_x86_ops->get_segment(vcpu, var, seg);
  3032. }
  3033. gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access)
  3034. {
  3035. gpa_t t_gpa;
  3036. struct x86_exception exception;
  3037. BUG_ON(!mmu_is_nested(vcpu));
  3038. /* NPT walks are always user-walks */
  3039. access |= PFERR_USER_MASK;
  3040. t_gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gpa, access, &exception);
  3041. return t_gpa;
  3042. }
  3043. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
  3044. struct x86_exception *exception)
  3045. {
  3046. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3047. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3048. }
  3049. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
  3050. struct x86_exception *exception)
  3051. {
  3052. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3053. access |= PFERR_FETCH_MASK;
  3054. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3055. }
  3056. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
  3057. struct x86_exception *exception)
  3058. {
  3059. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3060. access |= PFERR_WRITE_MASK;
  3061. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3062. }
  3063. /* uses this to access any guest's mapped memory without checking CPL */
  3064. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
  3065. struct x86_exception *exception)
  3066. {
  3067. return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
  3068. }
  3069. static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
  3070. struct kvm_vcpu *vcpu, u32 access,
  3071. struct x86_exception *exception)
  3072. {
  3073. void *data = val;
  3074. int r = X86EMUL_CONTINUE;
  3075. while (bytes) {
  3076. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
  3077. exception);
  3078. unsigned offset = addr & (PAGE_SIZE-1);
  3079. unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
  3080. int ret;
  3081. if (gpa == UNMAPPED_GVA)
  3082. return X86EMUL_PROPAGATE_FAULT;
  3083. ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
  3084. if (ret < 0) {
  3085. r = X86EMUL_IO_NEEDED;
  3086. goto out;
  3087. }
  3088. bytes -= toread;
  3089. data += toread;
  3090. addr += toread;
  3091. }
  3092. out:
  3093. return r;
  3094. }
  3095. /* used for instruction fetching */
  3096. static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
  3097. gva_t addr, void *val, unsigned int bytes,
  3098. struct x86_exception *exception)
  3099. {
  3100. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3101. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3102. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
  3103. access | PFERR_FETCH_MASK,
  3104. exception);
  3105. }
  3106. int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
  3107. gva_t addr, void *val, unsigned int bytes,
  3108. struct x86_exception *exception)
  3109. {
  3110. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3111. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3112. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
  3113. exception);
  3114. }
  3115. EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
  3116. static int kvm_read_guest_virt_system(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. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
  3122. }
  3123. int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
  3124. gva_t addr, void *val,
  3125. unsigned int bytes,
  3126. struct x86_exception *exception)
  3127. {
  3128. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3129. void *data = val;
  3130. int r = X86EMUL_CONTINUE;
  3131. while (bytes) {
  3132. gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
  3133. PFERR_WRITE_MASK,
  3134. exception);
  3135. unsigned offset = addr & (PAGE_SIZE-1);
  3136. unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
  3137. int ret;
  3138. if (gpa == UNMAPPED_GVA)
  3139. return X86EMUL_PROPAGATE_FAULT;
  3140. ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
  3141. if (ret < 0) {
  3142. r = X86EMUL_IO_NEEDED;
  3143. goto out;
  3144. }
  3145. bytes -= towrite;
  3146. data += towrite;
  3147. addr += towrite;
  3148. }
  3149. out:
  3150. return r;
  3151. }
  3152. EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
  3153. static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
  3154. gpa_t *gpa, struct x86_exception *exception,
  3155. bool write)
  3156. {
  3157. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  3158. if (vcpu_match_mmio_gva(vcpu, gva) &&
  3159. check_write_user_access(vcpu, write, access,
  3160. vcpu->arch.access)) {
  3161. *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
  3162. (gva & (PAGE_SIZE - 1));
  3163. trace_vcpu_match_mmio(gva, *gpa, write, false);
  3164. return 1;
  3165. }
  3166. if (write)
  3167. access |= PFERR_WRITE_MASK;
  3168. *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
  3169. if (*gpa == UNMAPPED_GVA)
  3170. return -1;
  3171. /* For APIC access vmexit */
  3172. if ((*gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3173. return 1;
  3174. if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
  3175. trace_vcpu_match_mmio(gva, *gpa, write, true);
  3176. return 1;
  3177. }
  3178. return 0;
  3179. }
  3180. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  3181. const void *val, int bytes)
  3182. {
  3183. int ret;
  3184. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  3185. if (ret < 0)
  3186. return 0;
  3187. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  3188. return 1;
  3189. }
  3190. struct read_write_emulator_ops {
  3191. int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
  3192. int bytes);
  3193. int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3194. void *val, int bytes);
  3195. int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3196. int bytes, void *val);
  3197. int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
  3198. void *val, int bytes);
  3199. bool write;
  3200. };
  3201. static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
  3202. {
  3203. if (vcpu->mmio_read_completed) {
  3204. memcpy(val, vcpu->mmio_data, bytes);
  3205. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
  3206. vcpu->mmio_phys_addr, *(u64 *)val);
  3207. vcpu->mmio_read_completed = 0;
  3208. return 1;
  3209. }
  3210. return 0;
  3211. }
  3212. static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
  3213. void *val, int bytes)
  3214. {
  3215. return !kvm_read_guest(vcpu->kvm, gpa, val, bytes);
  3216. }
  3217. static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
  3218. void *val, int bytes)
  3219. {
  3220. return emulator_write_phys(vcpu, gpa, val, bytes);
  3221. }
  3222. static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
  3223. {
  3224. trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
  3225. return vcpu_mmio_write(vcpu, gpa, bytes, val);
  3226. }
  3227. static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
  3228. void *val, int bytes)
  3229. {
  3230. trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
  3231. return X86EMUL_IO_NEEDED;
  3232. }
  3233. static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
  3234. void *val, int bytes)
  3235. {
  3236. memcpy(vcpu->mmio_data, val, bytes);
  3237. memcpy(vcpu->run->mmio.data, vcpu->mmio_data, 8);
  3238. return X86EMUL_CONTINUE;
  3239. }
  3240. static struct read_write_emulator_ops read_emultor = {
  3241. .read_write_prepare = read_prepare,
  3242. .read_write_emulate = read_emulate,
  3243. .read_write_mmio = vcpu_mmio_read,
  3244. .read_write_exit_mmio = read_exit_mmio,
  3245. };
  3246. static struct read_write_emulator_ops write_emultor = {
  3247. .read_write_emulate = write_emulate,
  3248. .read_write_mmio = write_mmio,
  3249. .read_write_exit_mmio = write_exit_mmio,
  3250. .write = true,
  3251. };
  3252. static int emulator_read_write_onepage(unsigned long addr, void *val,
  3253. unsigned int bytes,
  3254. struct x86_exception *exception,
  3255. struct kvm_vcpu *vcpu,
  3256. struct read_write_emulator_ops *ops)
  3257. {
  3258. gpa_t gpa;
  3259. int handled, ret;
  3260. bool write = ops->write;
  3261. if (ops->read_write_prepare &&
  3262. ops->read_write_prepare(vcpu, val, bytes))
  3263. return X86EMUL_CONTINUE;
  3264. ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
  3265. if (ret < 0)
  3266. return X86EMUL_PROPAGATE_FAULT;
  3267. /* For APIC access vmexit */
  3268. if (ret)
  3269. goto mmio;
  3270. if (ops->read_write_emulate(vcpu, gpa, val, bytes))
  3271. return X86EMUL_CONTINUE;
  3272. mmio:
  3273. /*
  3274. * Is this MMIO handled locally?
  3275. */
  3276. handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
  3277. if (handled == bytes)
  3278. return X86EMUL_CONTINUE;
  3279. gpa += handled;
  3280. bytes -= handled;
  3281. val += handled;
  3282. vcpu->mmio_needed = 1;
  3283. vcpu->run->exit_reason = KVM_EXIT_MMIO;
  3284. vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
  3285. vcpu->mmio_size = bytes;
  3286. vcpu->run->mmio.len = min(vcpu->mmio_size, 8);
  3287. vcpu->run->mmio.is_write = vcpu->mmio_is_write = write;
  3288. vcpu->mmio_index = 0;
  3289. return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
  3290. }
  3291. int emulator_read_write(struct x86_emulate_ctxt *ctxt, unsigned long addr,
  3292. void *val, unsigned int bytes,
  3293. struct x86_exception *exception,
  3294. struct read_write_emulator_ops *ops)
  3295. {
  3296. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3297. /* Crossing a page boundary? */
  3298. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  3299. int rc, now;
  3300. now = -addr & ~PAGE_MASK;
  3301. rc = emulator_read_write_onepage(addr, val, now, exception,
  3302. vcpu, ops);
  3303. if (rc != X86EMUL_CONTINUE)
  3304. return rc;
  3305. addr += now;
  3306. val += now;
  3307. bytes -= now;
  3308. }
  3309. return emulator_read_write_onepage(addr, val, bytes, exception,
  3310. vcpu, ops);
  3311. }
  3312. static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
  3313. unsigned long addr,
  3314. void *val,
  3315. unsigned int bytes,
  3316. struct x86_exception *exception)
  3317. {
  3318. return emulator_read_write(ctxt, addr, val, bytes,
  3319. exception, &read_emultor);
  3320. }
  3321. int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
  3322. unsigned long addr,
  3323. const void *val,
  3324. unsigned int bytes,
  3325. struct x86_exception *exception)
  3326. {
  3327. return emulator_read_write(ctxt, addr, (void *)val, bytes,
  3328. exception, &write_emultor);
  3329. }
  3330. #define CMPXCHG_TYPE(t, ptr, old, new) \
  3331. (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
  3332. #ifdef CONFIG_X86_64
  3333. # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
  3334. #else
  3335. # define CMPXCHG64(ptr, old, new) \
  3336. (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
  3337. #endif
  3338. static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
  3339. unsigned long addr,
  3340. const void *old,
  3341. const void *new,
  3342. unsigned int bytes,
  3343. struct x86_exception *exception)
  3344. {
  3345. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3346. gpa_t gpa;
  3347. struct page *page;
  3348. char *kaddr;
  3349. bool exchanged;
  3350. /* guests cmpxchg8b have to be emulated atomically */
  3351. if (bytes > 8 || (bytes & (bytes - 1)))
  3352. goto emul_write;
  3353. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
  3354. if (gpa == UNMAPPED_GVA ||
  3355. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3356. goto emul_write;
  3357. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  3358. goto emul_write;
  3359. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  3360. if (is_error_page(page)) {
  3361. kvm_release_page_clean(page);
  3362. goto emul_write;
  3363. }
  3364. kaddr = kmap_atomic(page, KM_USER0);
  3365. kaddr += offset_in_page(gpa);
  3366. switch (bytes) {
  3367. case 1:
  3368. exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
  3369. break;
  3370. case 2:
  3371. exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
  3372. break;
  3373. case 4:
  3374. exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
  3375. break;
  3376. case 8:
  3377. exchanged = CMPXCHG64(kaddr, old, new);
  3378. break;
  3379. default:
  3380. BUG();
  3381. }
  3382. kunmap_atomic(kaddr, KM_USER0);
  3383. kvm_release_page_dirty(page);
  3384. if (!exchanged)
  3385. return X86EMUL_CMPXCHG_FAILED;
  3386. kvm_mmu_pte_write(vcpu, gpa, new, bytes);
  3387. return X86EMUL_CONTINUE;
  3388. emul_write:
  3389. printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
  3390. return emulator_write_emulated(ctxt, addr, new, bytes, exception);
  3391. }
  3392. static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
  3393. {
  3394. /* TODO: String I/O for in kernel device */
  3395. int r;
  3396. if (vcpu->arch.pio.in)
  3397. r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
  3398. vcpu->arch.pio.size, pd);
  3399. else
  3400. r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
  3401. vcpu->arch.pio.port, vcpu->arch.pio.size,
  3402. pd);
  3403. return r;
  3404. }
  3405. static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
  3406. unsigned short port, void *val,
  3407. unsigned int count, bool in)
  3408. {
  3409. trace_kvm_pio(!in, port, size, count);
  3410. vcpu->arch.pio.port = port;
  3411. vcpu->arch.pio.in = in;
  3412. vcpu->arch.pio.count = count;
  3413. vcpu->arch.pio.size = size;
  3414. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  3415. vcpu->arch.pio.count = 0;
  3416. return 1;
  3417. }
  3418. vcpu->run->exit_reason = KVM_EXIT_IO;
  3419. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  3420. vcpu->run->io.size = size;
  3421. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  3422. vcpu->run->io.count = count;
  3423. vcpu->run->io.port = port;
  3424. return 0;
  3425. }
  3426. static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
  3427. int size, unsigned short port, void *val,
  3428. unsigned int count)
  3429. {
  3430. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3431. int ret;
  3432. if (vcpu->arch.pio.count)
  3433. goto data_avail;
  3434. ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
  3435. if (ret) {
  3436. data_avail:
  3437. memcpy(val, vcpu->arch.pio_data, size * count);
  3438. vcpu->arch.pio.count = 0;
  3439. return 1;
  3440. }
  3441. return 0;
  3442. }
  3443. static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
  3444. int size, unsigned short port,
  3445. const void *val, unsigned int count)
  3446. {
  3447. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3448. memcpy(vcpu->arch.pio_data, val, size * count);
  3449. return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
  3450. }
  3451. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3452. {
  3453. return kvm_x86_ops->get_segment_base(vcpu, seg);
  3454. }
  3455. static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
  3456. {
  3457. kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
  3458. }
  3459. int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
  3460. {
  3461. if (!need_emulate_wbinvd(vcpu))
  3462. return X86EMUL_CONTINUE;
  3463. if (kvm_x86_ops->has_wbinvd_exit()) {
  3464. int cpu = get_cpu();
  3465. cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
  3466. smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
  3467. wbinvd_ipi, NULL, 1);
  3468. put_cpu();
  3469. cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
  3470. } else
  3471. wbinvd();
  3472. return X86EMUL_CONTINUE;
  3473. }
  3474. EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
  3475. static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
  3476. {
  3477. kvm_emulate_wbinvd(emul_to_vcpu(ctxt));
  3478. }
  3479. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  3480. {
  3481. return _kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
  3482. }
  3483. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  3484. {
  3485. return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
  3486. }
  3487. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  3488. {
  3489. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  3490. }
  3491. static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
  3492. {
  3493. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3494. unsigned long value;
  3495. switch (cr) {
  3496. case 0:
  3497. value = kvm_read_cr0(vcpu);
  3498. break;
  3499. case 2:
  3500. value = vcpu->arch.cr2;
  3501. break;
  3502. case 3:
  3503. value = kvm_read_cr3(vcpu);
  3504. break;
  3505. case 4:
  3506. value = kvm_read_cr4(vcpu);
  3507. break;
  3508. case 8:
  3509. value = kvm_get_cr8(vcpu);
  3510. break;
  3511. default:
  3512. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  3513. return 0;
  3514. }
  3515. return value;
  3516. }
  3517. static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
  3518. {
  3519. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3520. int res = 0;
  3521. switch (cr) {
  3522. case 0:
  3523. res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
  3524. break;
  3525. case 2:
  3526. vcpu->arch.cr2 = val;
  3527. break;
  3528. case 3:
  3529. res = kvm_set_cr3(vcpu, val);
  3530. break;
  3531. case 4:
  3532. res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
  3533. break;
  3534. case 8:
  3535. res = kvm_set_cr8(vcpu, val);
  3536. break;
  3537. default:
  3538. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  3539. res = -1;
  3540. }
  3541. return res;
  3542. }
  3543. static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
  3544. {
  3545. return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
  3546. }
  3547. static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  3548. {
  3549. kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
  3550. }
  3551. static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  3552. {
  3553. kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
  3554. }
  3555. static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  3556. {
  3557. kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
  3558. }
  3559. static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
  3560. {
  3561. kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
  3562. }
  3563. static unsigned long emulator_get_cached_segment_base(
  3564. struct x86_emulate_ctxt *ctxt, int seg)
  3565. {
  3566. return get_segment_base(emul_to_vcpu(ctxt), seg);
  3567. }
  3568. static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
  3569. struct desc_struct *desc, u32 *base3,
  3570. int seg)
  3571. {
  3572. struct kvm_segment var;
  3573. kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
  3574. *selector = var.selector;
  3575. if (var.unusable)
  3576. return false;
  3577. if (var.g)
  3578. var.limit >>= 12;
  3579. set_desc_limit(desc, var.limit);
  3580. set_desc_base(desc, (unsigned long)var.base);
  3581. #ifdef CONFIG_X86_64
  3582. if (base3)
  3583. *base3 = var.base >> 32;
  3584. #endif
  3585. desc->type = var.type;
  3586. desc->s = var.s;
  3587. desc->dpl = var.dpl;
  3588. desc->p = var.present;
  3589. desc->avl = var.avl;
  3590. desc->l = var.l;
  3591. desc->d = var.db;
  3592. desc->g = var.g;
  3593. return true;
  3594. }
  3595. static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
  3596. struct desc_struct *desc, u32 base3,
  3597. int seg)
  3598. {
  3599. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3600. struct kvm_segment var;
  3601. var.selector = selector;
  3602. var.base = get_desc_base(desc);
  3603. #ifdef CONFIG_X86_64
  3604. var.base |= ((u64)base3) << 32;
  3605. #endif
  3606. var.limit = get_desc_limit(desc);
  3607. if (desc->g)
  3608. var.limit = (var.limit << 12) | 0xfff;
  3609. var.type = desc->type;
  3610. var.present = desc->p;
  3611. var.dpl = desc->dpl;
  3612. var.db = desc->d;
  3613. var.s = desc->s;
  3614. var.l = desc->l;
  3615. var.g = desc->g;
  3616. var.avl = desc->avl;
  3617. var.present = desc->p;
  3618. var.unusable = !var.present;
  3619. var.padding = 0;
  3620. kvm_set_segment(vcpu, &var, seg);
  3621. return;
  3622. }
  3623. static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
  3624. u32 msr_index, u64 *pdata)
  3625. {
  3626. return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
  3627. }
  3628. static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
  3629. u32 msr_index, u64 data)
  3630. {
  3631. return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
  3632. }
  3633. static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
  3634. u32 pmc, u64 *pdata)
  3635. {
  3636. return kvm_pmu_read_pmc(emul_to_vcpu(ctxt), pmc, pdata);
  3637. }
  3638. static void emulator_halt(struct x86_emulate_ctxt *ctxt)
  3639. {
  3640. emul_to_vcpu(ctxt)->arch.halt_request = 1;
  3641. }
  3642. static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
  3643. {
  3644. preempt_disable();
  3645. kvm_load_guest_fpu(emul_to_vcpu(ctxt));
  3646. /*
  3647. * CR0.TS may reference the host fpu state, not the guest fpu state,
  3648. * so it may be clear at this point.
  3649. */
  3650. clts();
  3651. }
  3652. static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
  3653. {
  3654. preempt_enable();
  3655. }
  3656. static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
  3657. struct x86_instruction_info *info,
  3658. enum x86_intercept_stage stage)
  3659. {
  3660. return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
  3661. }
  3662. static struct x86_emulate_ops emulate_ops = {
  3663. .read_std = kvm_read_guest_virt_system,
  3664. .write_std = kvm_write_guest_virt_system,
  3665. .fetch = kvm_fetch_guest_virt,
  3666. .read_emulated = emulator_read_emulated,
  3667. .write_emulated = emulator_write_emulated,
  3668. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  3669. .invlpg = emulator_invlpg,
  3670. .pio_in_emulated = emulator_pio_in_emulated,
  3671. .pio_out_emulated = emulator_pio_out_emulated,
  3672. .get_segment = emulator_get_segment,
  3673. .set_segment = emulator_set_segment,
  3674. .get_cached_segment_base = emulator_get_cached_segment_base,
  3675. .get_gdt = emulator_get_gdt,
  3676. .get_idt = emulator_get_idt,
  3677. .set_gdt = emulator_set_gdt,
  3678. .set_idt = emulator_set_idt,
  3679. .get_cr = emulator_get_cr,
  3680. .set_cr = emulator_set_cr,
  3681. .cpl = emulator_get_cpl,
  3682. .get_dr = emulator_get_dr,
  3683. .set_dr = emulator_set_dr,
  3684. .set_msr = emulator_set_msr,
  3685. .get_msr = emulator_get_msr,
  3686. .read_pmc = emulator_read_pmc,
  3687. .halt = emulator_halt,
  3688. .wbinvd = emulator_wbinvd,
  3689. .fix_hypercall = emulator_fix_hypercall,
  3690. .get_fpu = emulator_get_fpu,
  3691. .put_fpu = emulator_put_fpu,
  3692. .intercept = emulator_intercept,
  3693. };
  3694. static void cache_all_regs(struct kvm_vcpu *vcpu)
  3695. {
  3696. kvm_register_read(vcpu, VCPU_REGS_RAX);
  3697. kvm_register_read(vcpu, VCPU_REGS_RSP);
  3698. kvm_register_read(vcpu, VCPU_REGS_RIP);
  3699. vcpu->arch.regs_dirty = ~0;
  3700. }
  3701. static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
  3702. {
  3703. u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu, mask);
  3704. /*
  3705. * an sti; sti; sequence only disable interrupts for the first
  3706. * instruction. So, if the last instruction, be it emulated or
  3707. * not, left the system with the INT_STI flag enabled, it
  3708. * means that the last instruction is an sti. We should not
  3709. * leave the flag on in this case. The same goes for mov ss
  3710. */
  3711. if (!(int_shadow & mask))
  3712. kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
  3713. }
  3714. static void inject_emulated_exception(struct kvm_vcpu *vcpu)
  3715. {
  3716. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  3717. if (ctxt->exception.vector == PF_VECTOR)
  3718. kvm_propagate_fault(vcpu, &ctxt->exception);
  3719. else if (ctxt->exception.error_code_valid)
  3720. kvm_queue_exception_e(vcpu, ctxt->exception.vector,
  3721. ctxt->exception.error_code);
  3722. else
  3723. kvm_queue_exception(vcpu, ctxt->exception.vector);
  3724. }
  3725. static void init_decode_cache(struct x86_emulate_ctxt *ctxt,
  3726. const unsigned long *regs)
  3727. {
  3728. memset(&ctxt->twobyte, 0,
  3729. (void *)&ctxt->regs - (void *)&ctxt->twobyte);
  3730. memcpy(ctxt->regs, regs, sizeof(ctxt->regs));
  3731. ctxt->fetch.start = 0;
  3732. ctxt->fetch.end = 0;
  3733. ctxt->io_read.pos = 0;
  3734. ctxt->io_read.end = 0;
  3735. ctxt->mem_read.pos = 0;
  3736. ctxt->mem_read.end = 0;
  3737. }
  3738. static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
  3739. {
  3740. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  3741. int cs_db, cs_l;
  3742. /*
  3743. * TODO: fix emulate.c to use guest_read/write_register
  3744. * instead of direct ->regs accesses, can save hundred cycles
  3745. * on Intel for instructions that don't read/change RSP, for
  3746. * for example.
  3747. */
  3748. cache_all_regs(vcpu);
  3749. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  3750. ctxt->eflags = kvm_get_rflags(vcpu);
  3751. ctxt->eip = kvm_rip_read(vcpu);
  3752. ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
  3753. (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
  3754. cs_l ? X86EMUL_MODE_PROT64 :
  3755. cs_db ? X86EMUL_MODE_PROT32 :
  3756. X86EMUL_MODE_PROT16;
  3757. ctxt->guest_mode = is_guest_mode(vcpu);
  3758. init_decode_cache(ctxt, vcpu->arch.regs);
  3759. vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
  3760. }
  3761. int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
  3762. {
  3763. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  3764. int ret;
  3765. init_emulate_ctxt(vcpu);
  3766. ctxt->op_bytes = 2;
  3767. ctxt->ad_bytes = 2;
  3768. ctxt->_eip = ctxt->eip + inc_eip;
  3769. ret = emulate_int_real(ctxt, irq);
  3770. if (ret != X86EMUL_CONTINUE)
  3771. return EMULATE_FAIL;
  3772. ctxt->eip = ctxt->_eip;
  3773. memcpy(vcpu->arch.regs, ctxt->regs, sizeof ctxt->regs);
  3774. kvm_rip_write(vcpu, ctxt->eip);
  3775. kvm_set_rflags(vcpu, ctxt->eflags);
  3776. if (irq == NMI_VECTOR)
  3777. vcpu->arch.nmi_pending = 0;
  3778. else
  3779. vcpu->arch.interrupt.pending = false;
  3780. return EMULATE_DONE;
  3781. }
  3782. EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
  3783. static int handle_emulation_failure(struct kvm_vcpu *vcpu)
  3784. {
  3785. int r = EMULATE_DONE;
  3786. ++vcpu->stat.insn_emulation_fail;
  3787. trace_kvm_emulate_insn_failed(vcpu);
  3788. if (!is_guest_mode(vcpu)) {
  3789. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3790. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  3791. vcpu->run->internal.ndata = 0;
  3792. r = EMULATE_FAIL;
  3793. }
  3794. kvm_queue_exception(vcpu, UD_VECTOR);
  3795. return r;
  3796. }
  3797. static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t gva)
  3798. {
  3799. gpa_t gpa;
  3800. if (tdp_enabled)
  3801. return false;
  3802. /*
  3803. * if emulation was due to access to shadowed page table
  3804. * and it failed try to unshadow page and re-entetr the
  3805. * guest to let CPU execute the instruction.
  3806. */
  3807. if (kvm_mmu_unprotect_page_virt(vcpu, gva))
  3808. return true;
  3809. gpa = kvm_mmu_gva_to_gpa_system(vcpu, gva, NULL);
  3810. if (gpa == UNMAPPED_GVA)
  3811. return true; /* let cpu generate fault */
  3812. if (!kvm_is_error_hva(gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT)))
  3813. return true;
  3814. return false;
  3815. }
  3816. static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
  3817. unsigned long cr2, int emulation_type)
  3818. {
  3819. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  3820. unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
  3821. last_retry_eip = vcpu->arch.last_retry_eip;
  3822. last_retry_addr = vcpu->arch.last_retry_addr;
  3823. /*
  3824. * If the emulation is caused by #PF and it is non-page_table
  3825. * writing instruction, it means the VM-EXIT is caused by shadow
  3826. * page protected, we can zap the shadow page and retry this
  3827. * instruction directly.
  3828. *
  3829. * Note: if the guest uses a non-page-table modifying instruction
  3830. * on the PDE that points to the instruction, then we will unmap
  3831. * the instruction and go to an infinite loop. So, we cache the
  3832. * last retried eip and the last fault address, if we meet the eip
  3833. * and the address again, we can break out of the potential infinite
  3834. * loop.
  3835. */
  3836. vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
  3837. if (!(emulation_type & EMULTYPE_RETRY))
  3838. return false;
  3839. if (x86_page_table_writing_insn(ctxt))
  3840. return false;
  3841. if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
  3842. return false;
  3843. vcpu->arch.last_retry_eip = ctxt->eip;
  3844. vcpu->arch.last_retry_addr = cr2;
  3845. if (!vcpu->arch.mmu.direct_map)
  3846. gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
  3847. kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  3848. return true;
  3849. }
  3850. int x86_emulate_instruction(struct kvm_vcpu *vcpu,
  3851. unsigned long cr2,
  3852. int emulation_type,
  3853. void *insn,
  3854. int insn_len)
  3855. {
  3856. int r;
  3857. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  3858. bool writeback = true;
  3859. kvm_clear_exception_queue(vcpu);
  3860. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  3861. init_emulate_ctxt(vcpu);
  3862. ctxt->interruptibility = 0;
  3863. ctxt->have_exception = false;
  3864. ctxt->perm_ok = false;
  3865. ctxt->only_vendor_specific_insn
  3866. = emulation_type & EMULTYPE_TRAP_UD;
  3867. r = x86_decode_insn(ctxt, insn, insn_len);
  3868. trace_kvm_emulate_insn_start(vcpu);
  3869. ++vcpu->stat.insn_emulation;
  3870. if (r != EMULATION_OK) {
  3871. if (emulation_type & EMULTYPE_TRAP_UD)
  3872. return EMULATE_FAIL;
  3873. if (reexecute_instruction(vcpu, cr2))
  3874. return EMULATE_DONE;
  3875. if (emulation_type & EMULTYPE_SKIP)
  3876. return EMULATE_FAIL;
  3877. return handle_emulation_failure(vcpu);
  3878. }
  3879. }
  3880. if (emulation_type & EMULTYPE_SKIP) {
  3881. kvm_rip_write(vcpu, ctxt->_eip);
  3882. return EMULATE_DONE;
  3883. }
  3884. if (retry_instruction(ctxt, cr2, emulation_type))
  3885. return EMULATE_DONE;
  3886. /* this is needed for vmware backdoor interface to work since it
  3887. changes registers values during IO operation */
  3888. if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
  3889. vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
  3890. memcpy(ctxt->regs, vcpu->arch.regs, sizeof ctxt->regs);
  3891. }
  3892. restart:
  3893. r = x86_emulate_insn(ctxt);
  3894. if (r == EMULATION_INTERCEPTED)
  3895. return EMULATE_DONE;
  3896. if (r == EMULATION_FAILED) {
  3897. if (reexecute_instruction(vcpu, cr2))
  3898. return EMULATE_DONE;
  3899. return handle_emulation_failure(vcpu);
  3900. }
  3901. if (ctxt->have_exception) {
  3902. inject_emulated_exception(vcpu);
  3903. r = EMULATE_DONE;
  3904. } else if (vcpu->arch.pio.count) {
  3905. if (!vcpu->arch.pio.in)
  3906. vcpu->arch.pio.count = 0;
  3907. else
  3908. writeback = false;
  3909. r = EMULATE_DO_MMIO;
  3910. } else if (vcpu->mmio_needed) {
  3911. if (!vcpu->mmio_is_write)
  3912. writeback = false;
  3913. r = EMULATE_DO_MMIO;
  3914. } else if (r == EMULATION_RESTART)
  3915. goto restart;
  3916. else
  3917. r = EMULATE_DONE;
  3918. if (writeback) {
  3919. toggle_interruptibility(vcpu, ctxt->interruptibility);
  3920. kvm_set_rflags(vcpu, ctxt->eflags);
  3921. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3922. memcpy(vcpu->arch.regs, ctxt->regs, sizeof ctxt->regs);
  3923. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  3924. kvm_rip_write(vcpu, ctxt->eip);
  3925. } else
  3926. vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
  3927. return r;
  3928. }
  3929. EXPORT_SYMBOL_GPL(x86_emulate_instruction);
  3930. int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
  3931. {
  3932. unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3933. int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
  3934. size, port, &val, 1);
  3935. /* do not return to emulator after return from userspace */
  3936. vcpu->arch.pio.count = 0;
  3937. return ret;
  3938. }
  3939. EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
  3940. static void tsc_bad(void *info)
  3941. {
  3942. __this_cpu_write(cpu_tsc_khz, 0);
  3943. }
  3944. static void tsc_khz_changed(void *data)
  3945. {
  3946. struct cpufreq_freqs *freq = data;
  3947. unsigned long khz = 0;
  3948. if (data)
  3949. khz = freq->new;
  3950. else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  3951. khz = cpufreq_quick_get(raw_smp_processor_id());
  3952. if (!khz)
  3953. khz = tsc_khz;
  3954. __this_cpu_write(cpu_tsc_khz, khz);
  3955. }
  3956. static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  3957. void *data)
  3958. {
  3959. struct cpufreq_freqs *freq = data;
  3960. struct kvm *kvm;
  3961. struct kvm_vcpu *vcpu;
  3962. int i, send_ipi = 0;
  3963. /*
  3964. * We allow guests to temporarily run on slowing clocks,
  3965. * provided we notify them after, or to run on accelerating
  3966. * clocks, provided we notify them before. Thus time never
  3967. * goes backwards.
  3968. *
  3969. * However, we have a problem. We can't atomically update
  3970. * the frequency of a given CPU from this function; it is
  3971. * merely a notifier, which can be called from any CPU.
  3972. * Changing the TSC frequency at arbitrary points in time
  3973. * requires a recomputation of local variables related to
  3974. * the TSC for each VCPU. We must flag these local variables
  3975. * to be updated and be sure the update takes place with the
  3976. * new frequency before any guests proceed.
  3977. *
  3978. * Unfortunately, the combination of hotplug CPU and frequency
  3979. * change creates an intractable locking scenario; the order
  3980. * of when these callouts happen is undefined with respect to
  3981. * CPU hotplug, and they can race with each other. As such,
  3982. * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
  3983. * undefined; you can actually have a CPU frequency change take
  3984. * place in between the computation of X and the setting of the
  3985. * variable. To protect against this problem, all updates of
  3986. * the per_cpu tsc_khz variable are done in an interrupt
  3987. * protected IPI, and all callers wishing to update the value
  3988. * must wait for a synchronous IPI to complete (which is trivial
  3989. * if the caller is on the CPU already). This establishes the
  3990. * necessary total order on variable updates.
  3991. *
  3992. * Note that because a guest time update may take place
  3993. * anytime after the setting of the VCPU's request bit, the
  3994. * correct TSC value must be set before the request. However,
  3995. * to ensure the update actually makes it to any guest which
  3996. * starts running in hardware virtualization between the set
  3997. * and the acquisition of the spinlock, we must also ping the
  3998. * CPU after setting the request bit.
  3999. *
  4000. */
  4001. if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
  4002. return 0;
  4003. if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
  4004. return 0;
  4005. smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
  4006. raw_spin_lock(&kvm_lock);
  4007. list_for_each_entry(kvm, &vm_list, vm_list) {
  4008. kvm_for_each_vcpu(i, vcpu, kvm) {
  4009. if (vcpu->cpu != freq->cpu)
  4010. continue;
  4011. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  4012. if (vcpu->cpu != smp_processor_id())
  4013. send_ipi = 1;
  4014. }
  4015. }
  4016. raw_spin_unlock(&kvm_lock);
  4017. if (freq->old < freq->new && send_ipi) {
  4018. /*
  4019. * We upscale the frequency. Must make the guest
  4020. * doesn't see old kvmclock values while running with
  4021. * the new frequency, otherwise we risk the guest sees
  4022. * time go backwards.
  4023. *
  4024. * In case we update the frequency for another cpu
  4025. * (which might be in guest context) send an interrupt
  4026. * to kick the cpu out of guest context. Next time
  4027. * guest context is entered kvmclock will be updated,
  4028. * so the guest will not see stale values.
  4029. */
  4030. smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1);
  4031. }
  4032. return 0;
  4033. }
  4034. static struct notifier_block kvmclock_cpufreq_notifier_block = {
  4035. .notifier_call = kvmclock_cpufreq_notifier
  4036. };
  4037. static int kvmclock_cpu_notifier(struct notifier_block *nfb,
  4038. unsigned long action, void *hcpu)
  4039. {
  4040. unsigned int cpu = (unsigned long)hcpu;
  4041. switch (action) {
  4042. case CPU_ONLINE:
  4043. case CPU_DOWN_FAILED:
  4044. smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
  4045. break;
  4046. case CPU_DOWN_PREPARE:
  4047. smp_call_function_single(cpu, tsc_bad, NULL, 1);
  4048. break;
  4049. }
  4050. return NOTIFY_OK;
  4051. }
  4052. static struct notifier_block kvmclock_cpu_notifier_block = {
  4053. .notifier_call = kvmclock_cpu_notifier,
  4054. .priority = -INT_MAX
  4055. };
  4056. static void kvm_timer_init(void)
  4057. {
  4058. int cpu;
  4059. max_tsc_khz = tsc_khz;
  4060. register_hotcpu_notifier(&kvmclock_cpu_notifier_block);
  4061. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  4062. #ifdef CONFIG_CPU_FREQ
  4063. struct cpufreq_policy policy;
  4064. memset(&policy, 0, sizeof(policy));
  4065. cpu = get_cpu();
  4066. cpufreq_get_policy(&policy, cpu);
  4067. if (policy.cpuinfo.max_freq)
  4068. max_tsc_khz = policy.cpuinfo.max_freq;
  4069. put_cpu();
  4070. #endif
  4071. cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
  4072. CPUFREQ_TRANSITION_NOTIFIER);
  4073. }
  4074. pr_debug("kvm: max_tsc_khz = %ld\n", max_tsc_khz);
  4075. for_each_online_cpu(cpu)
  4076. smp_call_function_single(cpu, tsc_khz_changed, NULL, 1);
  4077. }
  4078. static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
  4079. int kvm_is_in_guest(void)
  4080. {
  4081. return __this_cpu_read(current_vcpu) != NULL;
  4082. }
  4083. static int kvm_is_user_mode(void)
  4084. {
  4085. int user_mode = 3;
  4086. if (__this_cpu_read(current_vcpu))
  4087. user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
  4088. return user_mode != 0;
  4089. }
  4090. static unsigned long kvm_get_guest_ip(void)
  4091. {
  4092. unsigned long ip = 0;
  4093. if (__this_cpu_read(current_vcpu))
  4094. ip = kvm_rip_read(__this_cpu_read(current_vcpu));
  4095. return ip;
  4096. }
  4097. static struct perf_guest_info_callbacks kvm_guest_cbs = {
  4098. .is_in_guest = kvm_is_in_guest,
  4099. .is_user_mode = kvm_is_user_mode,
  4100. .get_guest_ip = kvm_get_guest_ip,
  4101. };
  4102. void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
  4103. {
  4104. __this_cpu_write(current_vcpu, vcpu);
  4105. }
  4106. EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
  4107. void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
  4108. {
  4109. __this_cpu_write(current_vcpu, NULL);
  4110. }
  4111. EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
  4112. static void kvm_set_mmio_spte_mask(void)
  4113. {
  4114. u64 mask;
  4115. int maxphyaddr = boot_cpu_data.x86_phys_bits;
  4116. /*
  4117. * Set the reserved bits and the present bit of an paging-structure
  4118. * entry to generate page fault with PFER.RSV = 1.
  4119. */
  4120. mask = ((1ull << (62 - maxphyaddr + 1)) - 1) << maxphyaddr;
  4121. mask |= 1ull;
  4122. #ifdef CONFIG_X86_64
  4123. /*
  4124. * If reserved bit is not supported, clear the present bit to disable
  4125. * mmio page fault.
  4126. */
  4127. if (maxphyaddr == 52)
  4128. mask &= ~1ull;
  4129. #endif
  4130. kvm_mmu_set_mmio_spte_mask(mask);
  4131. }
  4132. int kvm_arch_init(void *opaque)
  4133. {
  4134. int r;
  4135. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  4136. if (kvm_x86_ops) {
  4137. printk(KERN_ERR "kvm: already loaded the other module\n");
  4138. r = -EEXIST;
  4139. goto out;
  4140. }
  4141. if (!ops->cpu_has_kvm_support()) {
  4142. printk(KERN_ERR "kvm: no hardware support\n");
  4143. r = -EOPNOTSUPP;
  4144. goto out;
  4145. }
  4146. if (ops->disabled_by_bios()) {
  4147. printk(KERN_ERR "kvm: disabled by bios\n");
  4148. r = -EOPNOTSUPP;
  4149. goto out;
  4150. }
  4151. r = kvm_mmu_module_init();
  4152. if (r)
  4153. goto out;
  4154. kvm_set_mmio_spte_mask();
  4155. kvm_init_msr_list();
  4156. kvm_x86_ops = ops;
  4157. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  4158. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  4159. kvm_timer_init();
  4160. perf_register_guest_info_callbacks(&kvm_guest_cbs);
  4161. if (cpu_has_xsave)
  4162. host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  4163. return 0;
  4164. out:
  4165. return r;
  4166. }
  4167. void kvm_arch_exit(void)
  4168. {
  4169. perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
  4170. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  4171. cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
  4172. CPUFREQ_TRANSITION_NOTIFIER);
  4173. unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block);
  4174. kvm_x86_ops = NULL;
  4175. kvm_mmu_module_exit();
  4176. }
  4177. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  4178. {
  4179. ++vcpu->stat.halt_exits;
  4180. if (irqchip_in_kernel(vcpu->kvm)) {
  4181. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  4182. return 1;
  4183. } else {
  4184. vcpu->run->exit_reason = KVM_EXIT_HLT;
  4185. return 0;
  4186. }
  4187. }
  4188. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  4189. int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  4190. {
  4191. u64 param, ingpa, outgpa, ret;
  4192. uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
  4193. bool fast, longmode;
  4194. int cs_db, cs_l;
  4195. /*
  4196. * hypercall generates UD from non zero cpl and real mode
  4197. * per HYPER-V spec
  4198. */
  4199. if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
  4200. kvm_queue_exception(vcpu, UD_VECTOR);
  4201. return 0;
  4202. }
  4203. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  4204. longmode = is_long_mode(vcpu) && cs_l == 1;
  4205. if (!longmode) {
  4206. param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
  4207. (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
  4208. ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
  4209. (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
  4210. outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
  4211. (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
  4212. }
  4213. #ifdef CONFIG_X86_64
  4214. else {
  4215. param = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4216. ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4217. outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
  4218. }
  4219. #endif
  4220. code = param & 0xffff;
  4221. fast = (param >> 16) & 0x1;
  4222. rep_cnt = (param >> 32) & 0xfff;
  4223. rep_idx = (param >> 48) & 0xfff;
  4224. trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
  4225. switch (code) {
  4226. case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
  4227. kvm_vcpu_on_spin(vcpu);
  4228. break;
  4229. default:
  4230. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  4231. break;
  4232. }
  4233. ret = res | (((u64)rep_done & 0xfff) << 32);
  4234. if (longmode) {
  4235. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  4236. } else {
  4237. kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
  4238. kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
  4239. }
  4240. return 1;
  4241. }
  4242. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  4243. {
  4244. unsigned long nr, a0, a1, a2, a3, ret;
  4245. int r = 1;
  4246. if (kvm_hv_hypercall_enabled(vcpu->kvm))
  4247. return kvm_hv_hypercall(vcpu);
  4248. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4249. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  4250. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4251. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4252. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  4253. trace_kvm_hypercall(nr, a0, a1, a2, a3);
  4254. if (!is_long_mode(vcpu)) {
  4255. nr &= 0xFFFFFFFF;
  4256. a0 &= 0xFFFFFFFF;
  4257. a1 &= 0xFFFFFFFF;
  4258. a2 &= 0xFFFFFFFF;
  4259. a3 &= 0xFFFFFFFF;
  4260. }
  4261. if (kvm_x86_ops->get_cpl(vcpu) != 0) {
  4262. ret = -KVM_EPERM;
  4263. goto out;
  4264. }
  4265. switch (nr) {
  4266. case KVM_HC_VAPIC_POLL_IRQ:
  4267. ret = 0;
  4268. break;
  4269. default:
  4270. ret = -KVM_ENOSYS;
  4271. break;
  4272. }
  4273. out:
  4274. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  4275. ++vcpu->stat.hypercalls;
  4276. return r;
  4277. }
  4278. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  4279. int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
  4280. {
  4281. struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
  4282. char instruction[3];
  4283. unsigned long rip = kvm_rip_read(vcpu);
  4284. /*
  4285. * Blow out the MMU to ensure that no other VCPU has an active mapping
  4286. * to ensure that the updated hypercall appears atomically across all
  4287. * VCPUs.
  4288. */
  4289. kvm_mmu_zap_all(vcpu->kvm);
  4290. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  4291. return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
  4292. }
  4293. /*
  4294. * Check if userspace requested an interrupt window, and that the
  4295. * interrupt window is open.
  4296. *
  4297. * No need to exit to userspace if we already have an interrupt queued.
  4298. */
  4299. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
  4300. {
  4301. return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
  4302. vcpu->run->request_interrupt_window &&
  4303. kvm_arch_interrupt_allowed(vcpu));
  4304. }
  4305. static void post_kvm_run_save(struct kvm_vcpu *vcpu)
  4306. {
  4307. struct kvm_run *kvm_run = vcpu->run;
  4308. kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  4309. kvm_run->cr8 = kvm_get_cr8(vcpu);
  4310. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  4311. if (irqchip_in_kernel(vcpu->kvm))
  4312. kvm_run->ready_for_interrupt_injection = 1;
  4313. else
  4314. kvm_run->ready_for_interrupt_injection =
  4315. kvm_arch_interrupt_allowed(vcpu) &&
  4316. !kvm_cpu_has_interrupt(vcpu) &&
  4317. !kvm_event_needs_reinjection(vcpu);
  4318. }
  4319. static void vapic_enter(struct kvm_vcpu *vcpu)
  4320. {
  4321. struct kvm_lapic *apic = vcpu->arch.apic;
  4322. struct page *page;
  4323. if (!apic || !apic->vapic_addr)
  4324. return;
  4325. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  4326. vcpu->arch.apic->vapic_page = page;
  4327. }
  4328. static void vapic_exit(struct kvm_vcpu *vcpu)
  4329. {
  4330. struct kvm_lapic *apic = vcpu->arch.apic;
  4331. int idx;
  4332. if (!apic || !apic->vapic_addr)
  4333. return;
  4334. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4335. kvm_release_page_dirty(apic->vapic_page);
  4336. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  4337. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4338. }
  4339. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  4340. {
  4341. int max_irr, tpr;
  4342. if (!kvm_x86_ops->update_cr8_intercept)
  4343. return;
  4344. if (!vcpu->arch.apic)
  4345. return;
  4346. if (!vcpu->arch.apic->vapic_addr)
  4347. max_irr = kvm_lapic_find_highest_irr(vcpu);
  4348. else
  4349. max_irr = -1;
  4350. if (max_irr != -1)
  4351. max_irr >>= 4;
  4352. tpr = kvm_lapic_get_cr8(vcpu);
  4353. kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
  4354. }
  4355. static void inject_pending_event(struct kvm_vcpu *vcpu)
  4356. {
  4357. /* try to reinject previous events if any */
  4358. if (vcpu->arch.exception.pending) {
  4359. trace_kvm_inj_exception(vcpu->arch.exception.nr,
  4360. vcpu->arch.exception.has_error_code,
  4361. vcpu->arch.exception.error_code);
  4362. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  4363. vcpu->arch.exception.has_error_code,
  4364. vcpu->arch.exception.error_code,
  4365. vcpu->arch.exception.reinject);
  4366. return;
  4367. }
  4368. if (vcpu->arch.nmi_injected) {
  4369. kvm_x86_ops->set_nmi(vcpu);
  4370. return;
  4371. }
  4372. if (vcpu->arch.interrupt.pending) {
  4373. kvm_x86_ops->set_irq(vcpu);
  4374. return;
  4375. }
  4376. /* try to inject new event if pending */
  4377. if (vcpu->arch.nmi_pending) {
  4378. if (kvm_x86_ops->nmi_allowed(vcpu)) {
  4379. --vcpu->arch.nmi_pending;
  4380. vcpu->arch.nmi_injected = true;
  4381. kvm_x86_ops->set_nmi(vcpu);
  4382. }
  4383. } else if (kvm_cpu_has_interrupt(vcpu)) {
  4384. if (kvm_x86_ops->interrupt_allowed(vcpu)) {
  4385. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
  4386. false);
  4387. kvm_x86_ops->set_irq(vcpu);
  4388. }
  4389. }
  4390. }
  4391. static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
  4392. {
  4393. if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
  4394. !vcpu->guest_xcr0_loaded) {
  4395. /* kvm_set_xcr() also depends on this */
  4396. xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
  4397. vcpu->guest_xcr0_loaded = 1;
  4398. }
  4399. }
  4400. static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
  4401. {
  4402. if (vcpu->guest_xcr0_loaded) {
  4403. if (vcpu->arch.xcr0 != host_xcr0)
  4404. xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
  4405. vcpu->guest_xcr0_loaded = 0;
  4406. }
  4407. }
  4408. static void process_nmi(struct kvm_vcpu *vcpu)
  4409. {
  4410. unsigned limit = 2;
  4411. /*
  4412. * x86 is limited to one NMI running, and one NMI pending after it.
  4413. * If an NMI is already in progress, limit further NMIs to just one.
  4414. * Otherwise, allow two (and we'll inject the first one immediately).
  4415. */
  4416. if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
  4417. limit = 1;
  4418. vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
  4419. vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
  4420. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4421. }
  4422. static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
  4423. {
  4424. int r;
  4425. bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
  4426. vcpu->run->request_interrupt_window;
  4427. bool req_immediate_exit = 0;
  4428. if (vcpu->requests) {
  4429. if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
  4430. kvm_mmu_unload(vcpu);
  4431. if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
  4432. __kvm_migrate_timers(vcpu);
  4433. if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
  4434. r = kvm_guest_time_update(vcpu);
  4435. if (unlikely(r))
  4436. goto out;
  4437. }
  4438. if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
  4439. kvm_mmu_sync_roots(vcpu);
  4440. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  4441. kvm_x86_ops->tlb_flush(vcpu);
  4442. if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
  4443. vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
  4444. r = 0;
  4445. goto out;
  4446. }
  4447. if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
  4448. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  4449. r = 0;
  4450. goto out;
  4451. }
  4452. if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
  4453. vcpu->fpu_active = 0;
  4454. kvm_x86_ops->fpu_deactivate(vcpu);
  4455. }
  4456. if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
  4457. /* Page is swapped out. Do synthetic halt */
  4458. vcpu->arch.apf.halted = true;
  4459. r = 1;
  4460. goto out;
  4461. }
  4462. if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
  4463. record_steal_time(vcpu);
  4464. if (kvm_check_request(KVM_REQ_NMI, vcpu))
  4465. process_nmi(vcpu);
  4466. req_immediate_exit =
  4467. kvm_check_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
  4468. if (kvm_check_request(KVM_REQ_PMU, vcpu))
  4469. kvm_handle_pmu_event(vcpu);
  4470. if (kvm_check_request(KVM_REQ_PMI, vcpu))
  4471. kvm_deliver_pmi(vcpu);
  4472. }
  4473. r = kvm_mmu_reload(vcpu);
  4474. if (unlikely(r))
  4475. goto out;
  4476. if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
  4477. inject_pending_event(vcpu);
  4478. /* enable NMI/IRQ window open exits if needed */
  4479. if (vcpu->arch.nmi_pending)
  4480. kvm_x86_ops->enable_nmi_window(vcpu);
  4481. else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
  4482. kvm_x86_ops->enable_irq_window(vcpu);
  4483. if (kvm_lapic_enabled(vcpu)) {
  4484. update_cr8_intercept(vcpu);
  4485. kvm_lapic_sync_to_vapic(vcpu);
  4486. }
  4487. }
  4488. preempt_disable();
  4489. kvm_x86_ops->prepare_guest_switch(vcpu);
  4490. if (vcpu->fpu_active)
  4491. kvm_load_guest_fpu(vcpu);
  4492. kvm_load_guest_xcr0(vcpu);
  4493. vcpu->mode = IN_GUEST_MODE;
  4494. /* We should set ->mode before check ->requests,
  4495. * see the comment in make_all_cpus_request.
  4496. */
  4497. smp_mb();
  4498. local_irq_disable();
  4499. if (vcpu->mode == EXITING_GUEST_MODE || vcpu->requests
  4500. || need_resched() || signal_pending(current)) {
  4501. vcpu->mode = OUTSIDE_GUEST_MODE;
  4502. smp_wmb();
  4503. local_irq_enable();
  4504. preempt_enable();
  4505. kvm_x86_ops->cancel_injection(vcpu);
  4506. r = 1;
  4507. goto out;
  4508. }
  4509. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  4510. if (req_immediate_exit)
  4511. smp_send_reschedule(vcpu->cpu);
  4512. kvm_guest_enter();
  4513. if (unlikely(vcpu->arch.switch_db_regs)) {
  4514. set_debugreg(0, 7);
  4515. set_debugreg(vcpu->arch.eff_db[0], 0);
  4516. set_debugreg(vcpu->arch.eff_db[1], 1);
  4517. set_debugreg(vcpu->arch.eff_db[2], 2);
  4518. set_debugreg(vcpu->arch.eff_db[3], 3);
  4519. }
  4520. trace_kvm_entry(vcpu->vcpu_id);
  4521. kvm_x86_ops->run(vcpu);
  4522. /*
  4523. * If the guest has used debug registers, at least dr7
  4524. * will be disabled while returning to the host.
  4525. * If we don't have active breakpoints in the host, we don't
  4526. * care about the messed up debug address registers. But if
  4527. * we have some of them active, restore the old state.
  4528. */
  4529. if (hw_breakpoint_active())
  4530. hw_breakpoint_restore();
  4531. vcpu->arch.last_guest_tsc = kvm_x86_ops->read_l1_tsc(vcpu);
  4532. vcpu->mode = OUTSIDE_GUEST_MODE;
  4533. smp_wmb();
  4534. local_irq_enable();
  4535. ++vcpu->stat.exits;
  4536. /*
  4537. * We must have an instruction between local_irq_enable() and
  4538. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  4539. * the interrupt shadow. The stat.exits increment will do nicely.
  4540. * But we need to prevent reordering, hence this barrier():
  4541. */
  4542. barrier();
  4543. kvm_guest_exit();
  4544. preempt_enable();
  4545. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  4546. /*
  4547. * Profile KVM exit RIPs:
  4548. */
  4549. if (unlikely(prof_on == KVM_PROFILING)) {
  4550. unsigned long rip = kvm_rip_read(vcpu);
  4551. profile_hit(KVM_PROFILING, (void *)rip);
  4552. }
  4553. kvm_lapic_sync_from_vapic(vcpu);
  4554. r = kvm_x86_ops->handle_exit(vcpu);
  4555. out:
  4556. return r;
  4557. }
  4558. static int __vcpu_run(struct kvm_vcpu *vcpu)
  4559. {
  4560. int r;
  4561. struct kvm *kvm = vcpu->kvm;
  4562. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  4563. pr_debug("vcpu %d received sipi with vector # %x\n",
  4564. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  4565. kvm_lapic_reset(vcpu);
  4566. r = kvm_arch_vcpu_reset(vcpu);
  4567. if (r)
  4568. return r;
  4569. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4570. }
  4571. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4572. vapic_enter(vcpu);
  4573. r = 1;
  4574. while (r > 0) {
  4575. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
  4576. !vcpu->arch.apf.halted)
  4577. r = vcpu_enter_guest(vcpu);
  4578. else {
  4579. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4580. kvm_vcpu_block(vcpu);
  4581. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4582. if (kvm_check_request(KVM_REQ_UNHALT, vcpu))
  4583. {
  4584. switch(vcpu->arch.mp_state) {
  4585. case KVM_MP_STATE_HALTED:
  4586. vcpu->arch.mp_state =
  4587. KVM_MP_STATE_RUNNABLE;
  4588. case KVM_MP_STATE_RUNNABLE:
  4589. vcpu->arch.apf.halted = false;
  4590. break;
  4591. case KVM_MP_STATE_SIPI_RECEIVED:
  4592. default:
  4593. r = -EINTR;
  4594. break;
  4595. }
  4596. }
  4597. }
  4598. if (r <= 0)
  4599. break;
  4600. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  4601. if (kvm_cpu_has_pending_timer(vcpu))
  4602. kvm_inject_pending_timer_irqs(vcpu);
  4603. if (dm_request_for_irq_injection(vcpu)) {
  4604. r = -EINTR;
  4605. vcpu->run->exit_reason = KVM_EXIT_INTR;
  4606. ++vcpu->stat.request_irq_exits;
  4607. }
  4608. kvm_check_async_pf_completion(vcpu);
  4609. if (signal_pending(current)) {
  4610. r = -EINTR;
  4611. vcpu->run->exit_reason = KVM_EXIT_INTR;
  4612. ++vcpu->stat.signal_exits;
  4613. }
  4614. if (need_resched()) {
  4615. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4616. kvm_resched(vcpu);
  4617. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4618. }
  4619. }
  4620. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4621. vapic_exit(vcpu);
  4622. return r;
  4623. }
  4624. static int complete_mmio(struct kvm_vcpu *vcpu)
  4625. {
  4626. struct kvm_run *run = vcpu->run;
  4627. int r;
  4628. if (!(vcpu->arch.pio.count || vcpu->mmio_needed))
  4629. return 1;
  4630. if (vcpu->mmio_needed) {
  4631. vcpu->mmio_needed = 0;
  4632. if (!vcpu->mmio_is_write)
  4633. memcpy(vcpu->mmio_data + vcpu->mmio_index,
  4634. run->mmio.data, 8);
  4635. vcpu->mmio_index += 8;
  4636. if (vcpu->mmio_index < vcpu->mmio_size) {
  4637. run->exit_reason = KVM_EXIT_MMIO;
  4638. run->mmio.phys_addr = vcpu->mmio_phys_addr + vcpu->mmio_index;
  4639. memcpy(run->mmio.data, vcpu->mmio_data + vcpu->mmio_index, 8);
  4640. run->mmio.len = min(vcpu->mmio_size - vcpu->mmio_index, 8);
  4641. run->mmio.is_write = vcpu->mmio_is_write;
  4642. vcpu->mmio_needed = 1;
  4643. return 0;
  4644. }
  4645. if (vcpu->mmio_is_write)
  4646. return 1;
  4647. vcpu->mmio_read_completed = 1;
  4648. }
  4649. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  4650. r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
  4651. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  4652. if (r != EMULATE_DONE)
  4653. return 0;
  4654. return 1;
  4655. }
  4656. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  4657. {
  4658. int r;
  4659. sigset_t sigsaved;
  4660. if (!tsk_used_math(current) && init_fpu(current))
  4661. return -ENOMEM;
  4662. if (vcpu->sigset_active)
  4663. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  4664. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  4665. kvm_vcpu_block(vcpu);
  4666. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  4667. r = -EAGAIN;
  4668. goto out;
  4669. }
  4670. /* re-sync apic's tpr */
  4671. if (!irqchip_in_kernel(vcpu->kvm)) {
  4672. if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
  4673. r = -EINVAL;
  4674. goto out;
  4675. }
  4676. }
  4677. r = complete_mmio(vcpu);
  4678. if (r <= 0)
  4679. goto out;
  4680. r = __vcpu_run(vcpu);
  4681. out:
  4682. post_kvm_run_save(vcpu);
  4683. if (vcpu->sigset_active)
  4684. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  4685. return r;
  4686. }
  4687. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  4688. {
  4689. if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
  4690. /*
  4691. * We are here if userspace calls get_regs() in the middle of
  4692. * instruction emulation. Registers state needs to be copied
  4693. * back from emulation context to vcpu. Usrapace shouldn't do
  4694. * that usually, but some bad designed PV devices (vmware
  4695. * backdoor interface) need this to work
  4696. */
  4697. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4698. memcpy(vcpu->arch.regs, ctxt->regs, sizeof ctxt->regs);
  4699. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  4700. }
  4701. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4702. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  4703. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4704. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4705. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  4706. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  4707. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  4708. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  4709. #ifdef CONFIG_X86_64
  4710. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  4711. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  4712. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  4713. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  4714. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  4715. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  4716. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  4717. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  4718. #endif
  4719. regs->rip = kvm_rip_read(vcpu);
  4720. regs->rflags = kvm_get_rflags(vcpu);
  4721. return 0;
  4722. }
  4723. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  4724. {
  4725. vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
  4726. vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
  4727. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  4728. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  4729. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  4730. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  4731. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  4732. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  4733. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  4734. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  4735. #ifdef CONFIG_X86_64
  4736. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  4737. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  4738. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  4739. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  4740. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  4741. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  4742. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  4743. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  4744. #endif
  4745. kvm_rip_write(vcpu, regs->rip);
  4746. kvm_set_rflags(vcpu, regs->rflags);
  4747. vcpu->arch.exception.pending = false;
  4748. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4749. return 0;
  4750. }
  4751. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4752. {
  4753. struct kvm_segment cs;
  4754. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4755. *db = cs.db;
  4756. *l = cs.l;
  4757. }
  4758. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  4759. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  4760. struct kvm_sregs *sregs)
  4761. {
  4762. struct desc_ptr dt;
  4763. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  4764. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  4765. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  4766. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  4767. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  4768. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  4769. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  4770. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  4771. kvm_x86_ops->get_idt(vcpu, &dt);
  4772. sregs->idt.limit = dt.size;
  4773. sregs->idt.base = dt.address;
  4774. kvm_x86_ops->get_gdt(vcpu, &dt);
  4775. sregs->gdt.limit = dt.size;
  4776. sregs->gdt.base = dt.address;
  4777. sregs->cr0 = kvm_read_cr0(vcpu);
  4778. sregs->cr2 = vcpu->arch.cr2;
  4779. sregs->cr3 = kvm_read_cr3(vcpu);
  4780. sregs->cr4 = kvm_read_cr4(vcpu);
  4781. sregs->cr8 = kvm_get_cr8(vcpu);
  4782. sregs->efer = vcpu->arch.efer;
  4783. sregs->apic_base = kvm_get_apic_base(vcpu);
  4784. memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
  4785. if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
  4786. set_bit(vcpu->arch.interrupt.nr,
  4787. (unsigned long *)sregs->interrupt_bitmap);
  4788. return 0;
  4789. }
  4790. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  4791. struct kvm_mp_state *mp_state)
  4792. {
  4793. mp_state->mp_state = vcpu->arch.mp_state;
  4794. return 0;
  4795. }
  4796. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  4797. struct kvm_mp_state *mp_state)
  4798. {
  4799. vcpu->arch.mp_state = mp_state->mp_state;
  4800. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4801. return 0;
  4802. }
  4803. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
  4804. bool has_error_code, u32 error_code)
  4805. {
  4806. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  4807. int ret;
  4808. init_emulate_ctxt(vcpu);
  4809. ret = emulator_task_switch(ctxt, tss_selector, reason,
  4810. has_error_code, error_code);
  4811. if (ret)
  4812. return EMULATE_FAIL;
  4813. memcpy(vcpu->arch.regs, ctxt->regs, sizeof ctxt->regs);
  4814. kvm_rip_write(vcpu, ctxt->eip);
  4815. kvm_set_rflags(vcpu, ctxt->eflags);
  4816. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4817. return EMULATE_DONE;
  4818. }
  4819. EXPORT_SYMBOL_GPL(kvm_task_switch);
  4820. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  4821. struct kvm_sregs *sregs)
  4822. {
  4823. int mmu_reset_needed = 0;
  4824. int pending_vec, max_bits, idx;
  4825. struct desc_ptr dt;
  4826. dt.size = sregs->idt.limit;
  4827. dt.address = sregs->idt.base;
  4828. kvm_x86_ops->set_idt(vcpu, &dt);
  4829. dt.size = sregs->gdt.limit;
  4830. dt.address = sregs->gdt.base;
  4831. kvm_x86_ops->set_gdt(vcpu, &dt);
  4832. vcpu->arch.cr2 = sregs->cr2;
  4833. mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
  4834. vcpu->arch.cr3 = sregs->cr3;
  4835. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4836. kvm_set_cr8(vcpu, sregs->cr8);
  4837. mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
  4838. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  4839. kvm_set_apic_base(vcpu, sregs->apic_base);
  4840. mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
  4841. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  4842. vcpu->arch.cr0 = sregs->cr0;
  4843. mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
  4844. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  4845. if (sregs->cr4 & X86_CR4_OSXSAVE)
  4846. kvm_update_cpuid(vcpu);
  4847. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4848. if (!is_long_mode(vcpu) && is_pae(vcpu)) {
  4849. load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
  4850. mmu_reset_needed = 1;
  4851. }
  4852. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4853. if (mmu_reset_needed)
  4854. kvm_mmu_reset_context(vcpu);
  4855. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  4856. pending_vec = find_first_bit(
  4857. (const unsigned long *)sregs->interrupt_bitmap, max_bits);
  4858. if (pending_vec < max_bits) {
  4859. kvm_queue_interrupt(vcpu, pending_vec, false);
  4860. pr_debug("Set back pending irq %d\n", pending_vec);
  4861. }
  4862. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  4863. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  4864. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  4865. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  4866. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  4867. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  4868. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  4869. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  4870. update_cr8_intercept(vcpu);
  4871. /* Older userspace won't unhalt the vcpu on reset. */
  4872. if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
  4873. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  4874. !is_protmode(vcpu))
  4875. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4876. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4877. return 0;
  4878. }
  4879. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  4880. struct kvm_guest_debug *dbg)
  4881. {
  4882. unsigned long rflags;
  4883. int i, r;
  4884. if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
  4885. r = -EBUSY;
  4886. if (vcpu->arch.exception.pending)
  4887. goto out;
  4888. if (dbg->control & KVM_GUESTDBG_INJECT_DB)
  4889. kvm_queue_exception(vcpu, DB_VECTOR);
  4890. else
  4891. kvm_queue_exception(vcpu, BP_VECTOR);
  4892. }
  4893. /*
  4894. * Read rflags as long as potentially injected trace flags are still
  4895. * filtered out.
  4896. */
  4897. rflags = kvm_get_rflags(vcpu);
  4898. vcpu->guest_debug = dbg->control;
  4899. if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
  4900. vcpu->guest_debug = 0;
  4901. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  4902. for (i = 0; i < KVM_NR_DB_REGS; ++i)
  4903. vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
  4904. vcpu->arch.switch_db_regs =
  4905. (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
  4906. } else {
  4907. for (i = 0; i < KVM_NR_DB_REGS; i++)
  4908. vcpu->arch.eff_db[i] = vcpu->arch.db[i];
  4909. vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
  4910. }
  4911. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  4912. vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
  4913. get_segment_base(vcpu, VCPU_SREG_CS);
  4914. /*
  4915. * Trigger an rflags update that will inject or remove the trace
  4916. * flags.
  4917. */
  4918. kvm_set_rflags(vcpu, rflags);
  4919. kvm_x86_ops->set_guest_debug(vcpu, dbg);
  4920. r = 0;
  4921. out:
  4922. return r;
  4923. }
  4924. /*
  4925. * Translate a guest virtual address to a guest physical address.
  4926. */
  4927. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  4928. struct kvm_translation *tr)
  4929. {
  4930. unsigned long vaddr = tr->linear_address;
  4931. gpa_t gpa;
  4932. int idx;
  4933. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4934. gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
  4935. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4936. tr->physical_address = gpa;
  4937. tr->valid = gpa != UNMAPPED_GVA;
  4938. tr->writeable = 1;
  4939. tr->usermode = 0;
  4940. return 0;
  4941. }
  4942. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  4943. {
  4944. struct i387_fxsave_struct *fxsave =
  4945. &vcpu->arch.guest_fpu.state->fxsave;
  4946. memcpy(fpu->fpr, fxsave->st_space, 128);
  4947. fpu->fcw = fxsave->cwd;
  4948. fpu->fsw = fxsave->swd;
  4949. fpu->ftwx = fxsave->twd;
  4950. fpu->last_opcode = fxsave->fop;
  4951. fpu->last_ip = fxsave->rip;
  4952. fpu->last_dp = fxsave->rdp;
  4953. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  4954. return 0;
  4955. }
  4956. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  4957. {
  4958. struct i387_fxsave_struct *fxsave =
  4959. &vcpu->arch.guest_fpu.state->fxsave;
  4960. memcpy(fxsave->st_space, fpu->fpr, 128);
  4961. fxsave->cwd = fpu->fcw;
  4962. fxsave->swd = fpu->fsw;
  4963. fxsave->twd = fpu->ftwx;
  4964. fxsave->fop = fpu->last_opcode;
  4965. fxsave->rip = fpu->last_ip;
  4966. fxsave->rdp = fpu->last_dp;
  4967. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  4968. return 0;
  4969. }
  4970. int fx_init(struct kvm_vcpu *vcpu)
  4971. {
  4972. int err;
  4973. err = fpu_alloc(&vcpu->arch.guest_fpu);
  4974. if (err)
  4975. return err;
  4976. fpu_finit(&vcpu->arch.guest_fpu);
  4977. /*
  4978. * Ensure guest xcr0 is valid for loading
  4979. */
  4980. vcpu->arch.xcr0 = XSTATE_FP;
  4981. vcpu->arch.cr0 |= X86_CR0_ET;
  4982. return 0;
  4983. }
  4984. EXPORT_SYMBOL_GPL(fx_init);
  4985. static void fx_free(struct kvm_vcpu *vcpu)
  4986. {
  4987. fpu_free(&vcpu->arch.guest_fpu);
  4988. }
  4989. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  4990. {
  4991. if (vcpu->guest_fpu_loaded)
  4992. return;
  4993. /*
  4994. * Restore all possible states in the guest,
  4995. * and assume host would use all available bits.
  4996. * Guest xcr0 would be loaded later.
  4997. */
  4998. kvm_put_guest_xcr0(vcpu);
  4999. vcpu->guest_fpu_loaded = 1;
  5000. unlazy_fpu(current);
  5001. fpu_restore_checking(&vcpu->arch.guest_fpu);
  5002. trace_kvm_fpu(1);
  5003. }
  5004. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  5005. {
  5006. kvm_put_guest_xcr0(vcpu);
  5007. if (!vcpu->guest_fpu_loaded)
  5008. return;
  5009. vcpu->guest_fpu_loaded = 0;
  5010. fpu_save_init(&vcpu->arch.guest_fpu);
  5011. ++vcpu->stat.fpu_reload;
  5012. kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
  5013. trace_kvm_fpu(0);
  5014. }
  5015. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  5016. {
  5017. kvmclock_reset(vcpu);
  5018. free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
  5019. fx_free(vcpu);
  5020. kvm_x86_ops->vcpu_free(vcpu);
  5021. }
  5022. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  5023. unsigned int id)
  5024. {
  5025. if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
  5026. printk_once(KERN_WARNING
  5027. "kvm: SMP vm created on host with unstable TSC; "
  5028. "guest TSC will not be reliable\n");
  5029. return kvm_x86_ops->vcpu_create(kvm, id);
  5030. }
  5031. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  5032. {
  5033. int r;
  5034. vcpu->arch.mtrr_state.have_fixed = 1;
  5035. vcpu_load(vcpu);
  5036. r = kvm_arch_vcpu_reset(vcpu);
  5037. if (r == 0)
  5038. r = kvm_mmu_setup(vcpu);
  5039. vcpu_put(vcpu);
  5040. return r;
  5041. }
  5042. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  5043. {
  5044. vcpu->arch.apf.msr_val = 0;
  5045. vcpu_load(vcpu);
  5046. kvm_mmu_unload(vcpu);
  5047. vcpu_put(vcpu);
  5048. fx_free(vcpu);
  5049. kvm_x86_ops->vcpu_free(vcpu);
  5050. }
  5051. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  5052. {
  5053. atomic_set(&vcpu->arch.nmi_queued, 0);
  5054. vcpu->arch.nmi_pending = 0;
  5055. vcpu->arch.nmi_injected = false;
  5056. vcpu->arch.switch_db_regs = 0;
  5057. memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
  5058. vcpu->arch.dr6 = DR6_FIXED_1;
  5059. vcpu->arch.dr7 = DR7_FIXED_1;
  5060. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5061. vcpu->arch.apf.msr_val = 0;
  5062. vcpu->arch.st.msr_val = 0;
  5063. kvmclock_reset(vcpu);
  5064. kvm_clear_async_pf_completion_queue(vcpu);
  5065. kvm_async_pf_hash_reset(vcpu);
  5066. vcpu->arch.apf.halted = false;
  5067. kvm_pmu_reset(vcpu);
  5068. return kvm_x86_ops->vcpu_reset(vcpu);
  5069. }
  5070. int kvm_arch_hardware_enable(void *garbage)
  5071. {
  5072. struct kvm *kvm;
  5073. struct kvm_vcpu *vcpu;
  5074. int i;
  5075. kvm_shared_msr_cpu_online();
  5076. list_for_each_entry(kvm, &vm_list, vm_list)
  5077. kvm_for_each_vcpu(i, vcpu, kvm)
  5078. if (vcpu->cpu == smp_processor_id())
  5079. kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
  5080. return kvm_x86_ops->hardware_enable(garbage);
  5081. }
  5082. void kvm_arch_hardware_disable(void *garbage)
  5083. {
  5084. kvm_x86_ops->hardware_disable(garbage);
  5085. drop_user_return_notifiers(garbage);
  5086. }
  5087. int kvm_arch_hardware_setup(void)
  5088. {
  5089. return kvm_x86_ops->hardware_setup();
  5090. }
  5091. void kvm_arch_hardware_unsetup(void)
  5092. {
  5093. kvm_x86_ops->hardware_unsetup();
  5094. }
  5095. void kvm_arch_check_processor_compat(void *rtn)
  5096. {
  5097. kvm_x86_ops->check_processor_compatibility(rtn);
  5098. }
  5099. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  5100. {
  5101. struct page *page;
  5102. struct kvm *kvm;
  5103. int r;
  5104. BUG_ON(vcpu->kvm == NULL);
  5105. kvm = vcpu->kvm;
  5106. vcpu->arch.emulate_ctxt.ops = &emulate_ops;
  5107. if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
  5108. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  5109. else
  5110. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  5111. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  5112. if (!page) {
  5113. r = -ENOMEM;
  5114. goto fail;
  5115. }
  5116. vcpu->arch.pio_data = page_address(page);
  5117. kvm_init_tsc_catchup(vcpu, max_tsc_khz);
  5118. r = kvm_mmu_create(vcpu);
  5119. if (r < 0)
  5120. goto fail_free_pio_data;
  5121. if (irqchip_in_kernel(kvm)) {
  5122. r = kvm_create_lapic(vcpu);
  5123. if (r < 0)
  5124. goto fail_mmu_destroy;
  5125. }
  5126. vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
  5127. GFP_KERNEL);
  5128. if (!vcpu->arch.mce_banks) {
  5129. r = -ENOMEM;
  5130. goto fail_free_lapic;
  5131. }
  5132. vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
  5133. if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL))
  5134. goto fail_free_mce_banks;
  5135. kvm_async_pf_hash_reset(vcpu);
  5136. kvm_pmu_init(vcpu);
  5137. return 0;
  5138. fail_free_mce_banks:
  5139. kfree(vcpu->arch.mce_banks);
  5140. fail_free_lapic:
  5141. kvm_free_lapic(vcpu);
  5142. fail_mmu_destroy:
  5143. kvm_mmu_destroy(vcpu);
  5144. fail_free_pio_data:
  5145. free_page((unsigned long)vcpu->arch.pio_data);
  5146. fail:
  5147. return r;
  5148. }
  5149. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  5150. {
  5151. int idx;
  5152. kvm_pmu_destroy(vcpu);
  5153. kfree(vcpu->arch.mce_banks);
  5154. kvm_free_lapic(vcpu);
  5155. idx = srcu_read_lock(&vcpu->kvm->srcu);
  5156. kvm_mmu_destroy(vcpu);
  5157. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  5158. free_page((unsigned long)vcpu->arch.pio_data);
  5159. }
  5160. int kvm_arch_init_vm(struct kvm *kvm)
  5161. {
  5162. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  5163. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  5164. /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
  5165. set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
  5166. raw_spin_lock_init(&kvm->arch.tsc_write_lock);
  5167. return 0;
  5168. }
  5169. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  5170. {
  5171. vcpu_load(vcpu);
  5172. kvm_mmu_unload(vcpu);
  5173. vcpu_put(vcpu);
  5174. }
  5175. static void kvm_free_vcpus(struct kvm *kvm)
  5176. {
  5177. unsigned int i;
  5178. struct kvm_vcpu *vcpu;
  5179. /*
  5180. * Unpin any mmu pages first.
  5181. */
  5182. kvm_for_each_vcpu(i, vcpu, kvm) {
  5183. kvm_clear_async_pf_completion_queue(vcpu);
  5184. kvm_unload_vcpu_mmu(vcpu);
  5185. }
  5186. kvm_for_each_vcpu(i, vcpu, kvm)
  5187. kvm_arch_vcpu_free(vcpu);
  5188. mutex_lock(&kvm->lock);
  5189. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  5190. kvm->vcpus[i] = NULL;
  5191. atomic_set(&kvm->online_vcpus, 0);
  5192. mutex_unlock(&kvm->lock);
  5193. }
  5194. void kvm_arch_sync_events(struct kvm *kvm)
  5195. {
  5196. kvm_free_all_assigned_devices(kvm);
  5197. kvm_free_pit(kvm);
  5198. }
  5199. void kvm_arch_destroy_vm(struct kvm *kvm)
  5200. {
  5201. kvm_iommu_unmap_guest(kvm);
  5202. kfree(kvm->arch.vpic);
  5203. kfree(kvm->arch.vioapic);
  5204. kvm_free_vcpus(kvm);
  5205. if (kvm->arch.apic_access_page)
  5206. put_page(kvm->arch.apic_access_page);
  5207. if (kvm->arch.ept_identity_pagetable)
  5208. put_page(kvm->arch.ept_identity_pagetable);
  5209. }
  5210. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  5211. struct kvm_memory_slot *memslot,
  5212. struct kvm_memory_slot old,
  5213. struct kvm_userspace_memory_region *mem,
  5214. int user_alloc)
  5215. {
  5216. int npages = memslot->npages;
  5217. int map_flags = MAP_PRIVATE | MAP_ANONYMOUS;
  5218. /* Prevent internal slot pages from being moved by fork()/COW. */
  5219. if (memslot->id >= KVM_MEMORY_SLOTS)
  5220. map_flags = MAP_SHARED | MAP_ANONYMOUS;
  5221. /*To keep backward compatibility with older userspace,
  5222. *x86 needs to hanlde !user_alloc case.
  5223. */
  5224. if (!user_alloc) {
  5225. if (npages && !old.rmap) {
  5226. unsigned long userspace_addr;
  5227. down_write(&current->mm->mmap_sem);
  5228. userspace_addr = do_mmap(NULL, 0,
  5229. npages * PAGE_SIZE,
  5230. PROT_READ | PROT_WRITE,
  5231. map_flags,
  5232. 0);
  5233. up_write(&current->mm->mmap_sem);
  5234. if (IS_ERR((void *)userspace_addr))
  5235. return PTR_ERR((void *)userspace_addr);
  5236. memslot->userspace_addr = userspace_addr;
  5237. }
  5238. }
  5239. return 0;
  5240. }
  5241. void kvm_arch_commit_memory_region(struct kvm *kvm,
  5242. struct kvm_userspace_memory_region *mem,
  5243. struct kvm_memory_slot old,
  5244. int user_alloc)
  5245. {
  5246. int nr_mmu_pages = 0, npages = mem->memory_size >> PAGE_SHIFT;
  5247. if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
  5248. int ret;
  5249. down_write(&current->mm->mmap_sem);
  5250. ret = do_munmap(current->mm, old.userspace_addr,
  5251. old.npages * PAGE_SIZE);
  5252. up_write(&current->mm->mmap_sem);
  5253. if (ret < 0)
  5254. printk(KERN_WARNING
  5255. "kvm_vm_ioctl_set_memory_region: "
  5256. "failed to munmap memory\n");
  5257. }
  5258. if (!kvm->arch.n_requested_mmu_pages)
  5259. nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  5260. spin_lock(&kvm->mmu_lock);
  5261. if (nr_mmu_pages)
  5262. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  5263. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  5264. spin_unlock(&kvm->mmu_lock);
  5265. }
  5266. void kvm_arch_flush_shadow(struct kvm *kvm)
  5267. {
  5268. kvm_mmu_zap_all(kvm);
  5269. kvm_reload_remote_mmus(kvm);
  5270. }
  5271. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  5272. {
  5273. return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
  5274. !vcpu->arch.apf.halted)
  5275. || !list_empty_careful(&vcpu->async_pf.done)
  5276. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
  5277. || atomic_read(&vcpu->arch.nmi_queued) ||
  5278. (kvm_arch_interrupt_allowed(vcpu) &&
  5279. kvm_cpu_has_interrupt(vcpu));
  5280. }
  5281. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  5282. {
  5283. int me;
  5284. int cpu = vcpu->cpu;
  5285. if (waitqueue_active(&vcpu->wq)) {
  5286. wake_up_interruptible(&vcpu->wq);
  5287. ++vcpu->stat.halt_wakeup;
  5288. }
  5289. me = get_cpu();
  5290. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  5291. if (kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE)
  5292. smp_send_reschedule(cpu);
  5293. put_cpu();
  5294. }
  5295. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
  5296. {
  5297. return kvm_x86_ops->interrupt_allowed(vcpu);
  5298. }
  5299. bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
  5300. {
  5301. unsigned long current_rip = kvm_rip_read(vcpu) +
  5302. get_segment_base(vcpu, VCPU_SREG_CS);
  5303. return current_rip == linear_rip;
  5304. }
  5305. EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
  5306. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
  5307. {
  5308. unsigned long rflags;
  5309. rflags = kvm_x86_ops->get_rflags(vcpu);
  5310. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  5311. rflags &= ~X86_EFLAGS_TF;
  5312. return rflags;
  5313. }
  5314. EXPORT_SYMBOL_GPL(kvm_get_rflags);
  5315. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  5316. {
  5317. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
  5318. kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
  5319. rflags |= X86_EFLAGS_TF;
  5320. kvm_x86_ops->set_rflags(vcpu, rflags);
  5321. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5322. }
  5323. EXPORT_SYMBOL_GPL(kvm_set_rflags);
  5324. void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
  5325. {
  5326. int r;
  5327. if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) ||
  5328. is_error_page(work->page))
  5329. return;
  5330. r = kvm_mmu_reload(vcpu);
  5331. if (unlikely(r))
  5332. return;
  5333. if (!vcpu->arch.mmu.direct_map &&
  5334. work->arch.cr3 != vcpu->arch.mmu.get_cr3(vcpu))
  5335. return;
  5336. vcpu->arch.mmu.page_fault(vcpu, work->gva, 0, true);
  5337. }
  5338. static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
  5339. {
  5340. return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
  5341. }
  5342. static inline u32 kvm_async_pf_next_probe(u32 key)
  5343. {
  5344. return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
  5345. }
  5346. static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  5347. {
  5348. u32 key = kvm_async_pf_hash_fn(gfn);
  5349. while (vcpu->arch.apf.gfns[key] != ~0)
  5350. key = kvm_async_pf_next_probe(key);
  5351. vcpu->arch.apf.gfns[key] = gfn;
  5352. }
  5353. static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
  5354. {
  5355. int i;
  5356. u32 key = kvm_async_pf_hash_fn(gfn);
  5357. for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
  5358. (vcpu->arch.apf.gfns[key] != gfn &&
  5359. vcpu->arch.apf.gfns[key] != ~0); i++)
  5360. key = kvm_async_pf_next_probe(key);
  5361. return key;
  5362. }
  5363. bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  5364. {
  5365. return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
  5366. }
  5367. static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  5368. {
  5369. u32 i, j, k;
  5370. i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
  5371. while (true) {
  5372. vcpu->arch.apf.gfns[i] = ~0;
  5373. do {
  5374. j = kvm_async_pf_next_probe(j);
  5375. if (vcpu->arch.apf.gfns[j] == ~0)
  5376. return;
  5377. k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
  5378. /*
  5379. * k lies cyclically in ]i,j]
  5380. * | i.k.j |
  5381. * |....j i.k.| or |.k..j i...|
  5382. */
  5383. } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
  5384. vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
  5385. i = j;
  5386. }
  5387. }
  5388. static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
  5389. {
  5390. return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
  5391. sizeof(val));
  5392. }
  5393. void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
  5394. struct kvm_async_pf *work)
  5395. {
  5396. struct x86_exception fault;
  5397. trace_kvm_async_pf_not_present(work->arch.token, work->gva);
  5398. kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
  5399. if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
  5400. (vcpu->arch.apf.send_user_only &&
  5401. kvm_x86_ops->get_cpl(vcpu) == 0))
  5402. kvm_make_request(KVM_REQ_APF_HALT, vcpu);
  5403. else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
  5404. fault.vector = PF_VECTOR;
  5405. fault.error_code_valid = true;
  5406. fault.error_code = 0;
  5407. fault.nested_page_fault = false;
  5408. fault.address = work->arch.token;
  5409. kvm_inject_page_fault(vcpu, &fault);
  5410. }
  5411. }
  5412. void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
  5413. struct kvm_async_pf *work)
  5414. {
  5415. struct x86_exception fault;
  5416. trace_kvm_async_pf_ready(work->arch.token, work->gva);
  5417. if (is_error_page(work->page))
  5418. work->arch.token = ~0; /* broadcast wakeup */
  5419. else
  5420. kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
  5421. if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
  5422. !apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
  5423. fault.vector = PF_VECTOR;
  5424. fault.error_code_valid = true;
  5425. fault.error_code = 0;
  5426. fault.nested_page_fault = false;
  5427. fault.address = work->arch.token;
  5428. kvm_inject_page_fault(vcpu, &fault);
  5429. }
  5430. vcpu->arch.apf.halted = false;
  5431. }
  5432. bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
  5433. {
  5434. if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
  5435. return true;
  5436. else
  5437. return !kvm_event_needs_reinjection(vcpu) &&
  5438. kvm_x86_ops->interrupt_allowed(vcpu);
  5439. }
  5440. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  5441. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
  5442. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
  5443. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
  5444. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
  5445. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
  5446. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
  5447. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
  5448. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
  5449. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
  5450. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
  5451. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);