x86.c 147 KB

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