x86.c 165 KB

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