x86.c 192 KB

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