x86.c 170 KB

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