x86.c 147 KB

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