x86.c 219 KB

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