x86.c 164 KB

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