x86.c 192 KB

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