x86.c 157 KB

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