x86.c 159 KB

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