x86.c 156 KB

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