x86.c 133 KB

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