x86.c 148 KB

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