x86.c 165 KB

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