x86.c 169 KB

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