x86.c 173 KB

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