x86.c 142 KB

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