x86.c 156 KB

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